summaryrefslogtreecommitdiff
path: root/agent/mibgroup/host/hr_print.c
blob: e7660ff39f4a37a542f30c71bf4a1ca507495d91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 *  Host Resources MIB - printer device group implementation - hr_print.c
 *
 */

#include <net-snmp/net-snmp-config.h>
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "host_res.h"
#include "hr_print.h"
#include "struct.h"
#include "util_funcs.h"

#define HRPRINT_MONOTONICALLY_INCREASING

        /*********************
	 *
	 *  Kernel & interface information,
	 *   and internal forward declarations
	 *
	 *********************/

void            Init_HR_Print(void);
int             Get_Next_HR_Print(void);
void            Save_HR_Print(void);
const char     *describe_printer(int);
int             printer_status(int);
int             printer_detail_status(int);
int             printer_errors(int);
int             header_hrprint(struct variable *, oid *, size_t *, int,
                               size_t *, WriteMethod **);
FILE           *run_lpstat(int *);


        /*********************
	 *
	 *  Initialisation & common implementation functions
	 *
	 *********************/

#define	HRPRINT_STATUS		1
#define	HRPRINT_ERROR		2

struct variable4 hrprint_variables[] = {
    {HRPRINT_STATUS, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
     var_hrprint, 2, {1, 1}},
    {HRPRINT_ERROR, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
     var_hrprint, 2, {1, 2}}
};
oid             hrprint_variables_oid[] = { 1, 3, 6, 1, 2, 1, 25, 3, 5 };


void
init_hr_print(void)
{
    init_device[HRDEV_PRINTER] = Init_HR_Print;
    next_device[HRDEV_PRINTER] = Get_Next_HR_Print;
    /*
     * save_device[ HRDEV_PRINTER ] = Save_HR_Print;        
     */
#ifdef HRPRINT_MONOTONICALLY_INCREASING
    dev_idx_inc[HRDEV_PRINTER] = 1;
#endif

    device_descr[HRDEV_PRINTER] = describe_printer;
    device_status[HRDEV_PRINTER] = printer_status;
    device_errors[HRDEV_PRINTER] = printer_errors;

    REGISTER_MIB("host/hr_print", hrprint_variables, variable4,
                 hrprint_variables_oid);
}

/*
 * header_hrprint(...
 * Arguments:
 * vp     IN      - pointer to variable entry that points here
 * name    IN/OUT  - IN/name requested, OUT/name found
 * length  IN/OUT  - length of IN/OUT oid's 
 * exact   IN      - TRUE if an exact match was requested
 * var_len OUT     - length of variable or 0 if function returned
 * write_method
 * 
 */
int
header_hrprint(struct variable *vp,
               oid * name,
               size_t * length,
               int exact, size_t * var_len, WriteMethod ** write_method)
{
#define HRPRINT_ENTRY_NAME_LENGTH	11
    oid             newname[MAX_OID_LEN];
    int             print_idx, LowIndex = -1;
    int             result;

    DEBUGMSGTL(("host/hr_print", "var_hrprint: "));
    DEBUGMSGOID(("host/hr_print", name, *length));
    DEBUGMSG(("host/hr_print", " %d\n", exact));

    memcpy((char *) newname, (char *) vp->name, vp->namelen * sizeof(oid));
    /*
     * Find "next" print entry 
     */

    Init_HR_Print();
    for (;;) {
        print_idx = Get_Next_HR_Print();
        if (print_idx == -1)
            break;
        newname[HRPRINT_ENTRY_NAME_LENGTH] = print_idx;
        result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
        if (exact && (result == 0)) {
            LowIndex = print_idx;
            /*
             * Save printer status information 
             */
            break;
        }
        if ((!exact && (result < 0)) &&
            (LowIndex == -1 || print_idx < LowIndex)) {
            LowIndex = print_idx;
            /*
             * Save printer status information 
             */
#ifdef HRPRINT_MONOTONICALLY_INCREASING
            break;
#endif
        }
    }

    if (LowIndex == -1) {
        DEBUGMSGTL(("host/hr_print", "... index out of range\n"));
        return (MATCH_FAILED);
    }

    memcpy((char *) name, (char *) newname,
           (vp->namelen + 1) * sizeof(oid));
    *length = vp->namelen + 1;
    *write_method = (WriteMethod*)0;
    *var_len = sizeof(long);    /* default to 'long' results */

    DEBUGMSGTL(("host/hr_print", "... get print stats "));
    DEBUGMSGOID(("host/hr_print", name, *length));
    DEBUGMSG(("host/hr_print", "\n"));
    return LowIndex;
}


        /*********************
	 *
	 *  System specific implementation functions
	 *
	 *********************/


u_char         *
var_hrprint(struct variable * vp,
            oid * name,
            size_t * length,
            int exact, size_t * var_len, WriteMethod ** write_method)
{
    int             print_idx;

    print_idx =
        header_hrprint(vp, name, length, exact, var_len, write_method);
    if (print_idx == MATCH_FAILED)
        return NULL;


    switch (vp->magic) {
    case HRPRINT_STATUS:
        long_return = printer_detail_status(print_idx);
        return (u_char *) & long_return;
    case HRPRINT_ERROR:
#if NETSNMP_NO_DUMMY_VALUES
        return NULL;
#else
        long_return = 0;        /* Null string */
        return (u_char *) & long_return;
#endif
    default:
        DEBUGMSGTL(("host/hr_print", "unknown sub-id %d in var_hrprint\n",
                    vp->magic));
    }
    return NULL;
}


        /*********************
	 *
	 *  Internal implementation functions
	 *
	 *********************/

static int      HRP_index;
static char   **HRP_name;
static int      HRP_nbrnames, HRP_maxnames;

#define HRP_MAX_INCR 10

void
Init_HR_Print(void)
{
#if HAVE_LPSTAT || HAVE_CGETNEXT || HAVE_PRINTCAP
    int             i;
#if HAVE_PRINTCAP
    FILE           *p;
#elif HAVE_CGETNEXT
    const char     *caps[] = { "/etc/printcap", NULL };
#elif HAVE_LPSTAT
    int	            fd;
    FILE           *p;
#endif

    HRP_index = 0;		/* fail safe at Get_Next_HR_Print */

    if (HRP_name) {
        for (i = 0; i < HRP_nbrnames; i++)
            free(HRP_name[i]);
        HRP_nbrnames = 0;
        HRP_maxnames = 0;
        SNMP_FREE(HRP_name);
    }

#if HAVE_PRINTCAP
    if ((p = fopen("/etc/printcap", "r")) != NULL) {
        char            buf[BUFSIZ], *ptr;
        while (fgets(buf, sizeof buf, p)) {
            buf[strlen(buf) - 1] = 0;
            if (buf[0] == '#' || buf[0] == 0 || buf[0] == ' '
                || buf[0] == '\t')
                continue;
            if ((ptr = strchr(buf, '\\')))
                *ptr = 0;
            if ((ptr = strchr(buf, ':')))
                *ptr = 0;
            if ((ptr = strchr(buf, '|')))
                *ptr = 0;
            ptr = buf;
#elif HAVE_CGETNEXT
    {
        char           *buf = NULL, *ptr;
        while (cgetnext(&buf, caps) > 0) {
            if ((ptr = strchr(buf, ':')))
                *ptr = 0;
            if ((ptr = strchr(buf, '|')))
                *ptr = 0;
            ptr = buf;
#elif HAVE_LPSTAT
    if ((p = run_lpstat(&fd)) != NULL) {
        char            buf[BUFSIZ], ptr[BUFSIZ];
        while (fgets(buf, sizeof buf, p)) {
            sscanf(buf, "%*s %*s %[^:]", ptr);
#endif
            if (HRP_nbrnames == HRP_maxnames) {
                char          **tmp;
                tmp = (char **) calloc(HRP_maxnames + HRP_MAX_INCR, sizeof(char *));
                if (!tmp)
                    goto finish;
		if (HRP_name) {
			memcpy(tmp, HRP_name, HRP_nbrnames * sizeof(char *));
			free(HRP_name);
		}
                HRP_maxnames += HRP_MAX_INCR;
                HRP_name = tmp;
            }
            HRP_name[HRP_nbrnames++] = strdup(ptr);
#if !defined(HAVE_PRINTCAP) && defined(HAVE_CGETNEXT)
            if (buf)
                free(buf);
#endif
        }
finish:
#if HAVE_PRINTCAP
        fclose(p);
#elif HAVE_CGETNEXT
        cgetclose();
#elif HAVE_LPSTAT
        fclose(p);
        close(fd);
#endif
    }
#endif                          /* HAVE_anything */
}

int
Get_Next_HR_Print(void)
{
    /*
     * The initial implementation system
     *   has no printers attached, and I've
     *   no real idea how to detect them,
     *   so don't bother.
     */
    if (HRP_index < HRP_nbrnames)  /* No printer */
        return (HRDEV_PRINTER << HRDEV_TYPE_SHIFT) + HRP_index++;
    else
        return -1;
}

const char     *
describe_printer(int idx)
{
    if (HRP_index == 0)  /* return empty string if not initialized */
	return "";

    DEBUGMSGTL(("host/hr_print", "describe p: %d/%d %s\n", HRP_index, idx,
                HRP_name[HRP_index - 1]));
    return HRP_name[HRP_index - 1];
}

int
printer_status(int idx)
{
    /*
     * hrDeviceStatus OBJECT-TYPE
     * SYNTAX     INTEGER {
     * unknown(1), running(2), warning(3), testing(4), down(5)
     * }
     */

    return 1;                   /* unknown */
}

int
printer_detail_status(int idx)
{
    /*
     * hrPrinterStatus OBJECT-TYPE
     * SYNTAX     INTEGER {
     * other(1), unknown(2), idle(3), printing(4), warmup(5)
     * }
     */

    return 2;                   /* unknown */
}

int
printer_errors(int idx)
{
    return 0;
}

#ifdef        HAVE_LPSTAT
/*
 * Run the lpstat command. If compiled with EXCACHE support, this
 * will actually cache the output for a while which helps a lot
 * with snmpbulkwalk (in fact, it keeps the client from exiting
 * due to timeouts).
 */
FILE           *
run_lpstat(int *fd)
{
    struct extensible ex;

    memset(&ex, 0, sizeof(ex));
    strcpy(ex.command, LPSTAT_PATH " -v");
    if ((*fd = get_exec_output(&ex)) < 0)
        return NULL;

    return fdopen(*fd, "r");
}
#endif