Debian Codegraph @

agent/mibgroup/host/hr_print.c

raw
1 /*
2 * Host Resources MIB - printer device group implementation - hr_print.c
3 *
4 */
5
6 #include <net-snmp/net-snmp-config.h>
7 #ifdef HAVE_STRING_H
8 #include <string.h>
9 #else
10 #include <strings.h>
11 #endif
12 #ifdef HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15 #include "host_res.h"
16 #include "hr_print.h"
17 #include "struct.h"
18 #include "util_funcs.h"
19
20 #define HRPRINT_MONOTONICALLY_INCREASING
21
22 /*********************
23 *
24 * Kernel & interface information,
25 * and internal forward declarations
26 *
27 *********************/
28
29 void Init_HR_Print(void);
30 int Get_Next_HR_Print(void);
31 const char *describe_printer(int);
32 int printer_status(int);
33 int printer_detail_status(int);
34 int printer_errors(int);
35 int header_hrprint(struct variable *, oid *, size_t *, int,
36 size_t *, WriteMethod **);
37 FILE *run_lpstat(int *);
38
39
40 /*********************
41 *
42 * Initialisation & common implementation functions
43 *
44 *********************/
45
46 #define HRPRINT_STATUS 1
47 #define HRPRINT_ERROR 2
48
49 struct variable4 hrprint_variables[] = {
50 {HRPRINT_STATUS, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
51 var_hrprint, 2, {1, 1}},
52 {HRPRINT_ERROR, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
53 var_hrprint, 2, {1, 2}}
54 };
55 oid hrprint_variables_oid[] = { 1, 3, 6, 1, 2, 1, 25, 3, 5 };
56
57
58 void
59 init_hr_print(void)
60 {
61 init_device[HRDEV_PRINTER] = Init_HR_Print;
62 next_device[HRDEV_PRINTER] = Get_Next_HR_Print;
63 /*
64 * save_device[ HRDEV_PRINTER ] = Save_HR_Print;
65 */
66 #ifdef HRPRINT_MONOTONICALLY_INCREASING
67 dev_idx_inc[HRDEV_PRINTER] = 1;
68 #endif
69
70 device_descr[HRDEV_PRINTER] = describe_printer;
71 device_status[HRDEV_PRINTER] = printer_status;
72 device_errors[HRDEV_PRINTER] = printer_errors;
73
74 REGISTER_MIB("host/hr_print", hrprint_variables, variable4,
75 hrprint_variables_oid);
76 }
77
78 /*
79 * header_hrprint(...
80 * Arguments:
81 * vp IN - pointer to variable entry that points here
82 * name IN/OUT - IN/name requested, OUT/name found
83 * length IN/OUT - length of IN/OUT oid's
84 * exact IN - TRUE if an exact match was requested
85 * var_len OUT - length of variable or 0 if function returned
86 * write_method
87 *
88 */
89 int
90 header_hrprint(struct variable *vp,
91 oid * name,
92 size_t * length,
93 int exact, size_t * var_len, WriteMethod ** write_method)
94 {
95 #define HRPRINT_ENTRY_NAME_LENGTH 11
96 oid newname[MAX_OID_LEN];
97 int print_idx, LowIndex = -1;
98 int result;
99
100 DEBUGMSGTL(("host/hr_print", "var_hrprint: "));
101 DEBUGMSGOID(("host/hr_print", name, *length));
102 DEBUGMSG(("host/hr_print", " %d\n", exact));
103
104 memcpy((char *) newname, (char *) vp->name, vp->namelen * sizeof(oid));
105 /*
106 * Find "next" print entry
107 */
108
109 Init_HR_Print();
110 for (;;) {
111 print_idx = Get_Next_HR_Print();
112 if (print_idx == -1)
113 break;
114 newname[HRPRINT_ENTRY_NAME_LENGTH] = print_idx;
115 result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
116 if (exact && (result == 0)) {
117 LowIndex = print_idx;
118 /*
119 * Save printer status information
120 */
121 break;
122 }
123 if ((!exact && (result < 0)) &&
124 (LowIndex == -1 || print_idx < LowIndex)) {
125 LowIndex = print_idx;
126 /*
127 * Save printer status information
128 */
129 #ifdef HRPRINT_MONOTONICALLY_INCREASING
130 break;
131 #endif
132 }
133 }
134
135 if (LowIndex == -1) {
136 DEBUGMSGTL(("host/hr_print", "... index out of range\n"));
137 return (MATCH_FAILED);
138 }
139
140 memcpy((char *) name, (char *) newname,
141 (vp->namelen + 1) * sizeof(oid));
142 *length = vp->namelen + 1;
143 *write_method = (WriteMethod*)0;
144 *var_len = sizeof(long); /* default to 'long' results */
145
146 DEBUGMSGTL(("host/hr_print", "... get print stats "));
147 DEBUGMSGOID(("host/hr_print", name, *length));
148 DEBUGMSG(("host/hr_print", "\n"));
149 return LowIndex;
150 }
151
152
153 /*********************
154 *
155 * System specific implementation functions
156 *
157 *********************/
158
159
160 u_char *
161 var_hrprint(struct variable * vp,
162 oid * name,
163 size_t * length,
164 int exact, size_t * var_len, WriteMethod ** write_method)
165 {
166 int print_idx;
167
168 print_idx =
169 header_hrprint(vp, name, length, exact, var_len, write_method);
170 if (print_idx == MATCH_FAILED)
171 return NULL;
172
173
174 switch (vp->magic) {
175 case HRPRINT_STATUS:
176 long_return = printer_detail_status(print_idx);
177 return (u_char *) & long_return;
178 case HRPRINT_ERROR:
179 #ifdef NETSNMP_NO_DUMMY_VALUES
180 return NULL;
181 #else
182 long_return = 0; /* Null string */
183 return (u_char *) & long_return;
184 #endif
185 default:
186 DEBUGMSGTL(("host/hr_print", "unknown sub-id %d in var_hrprint\n",
187 vp->magic));
188 }
189 return NULL;
190 }
191
192
193 /*********************
194 *
195 * Internal implementation functions
196 *
197 *********************/
198
199 static int HRP_index;
200 static char **HRP_name;
201 static int HRP_nbrnames;
202 #if defined(HAVE_LPSTAT) || defined(HAVE_CGETNEXT) || defined(HAVE_PRINTCAP)
203 static int HRP_maxnames;
204 #endif
205
206 #define HRP_MAX_INCR 10
207
208 void
209 Init_HR_Print(void)
210 {
211 #if defined(HAVE_LPSTAT) || defined(HAVE_CGETNEXT) || defined(HAVE_PRINTCAP)
212 int i;
213 #if defined(HAVE_PRINTCAP)
214 FILE *p;
215 #elif defined(HAVE_CGETNEXT)
216 const char *caps[] = { "/etc/printcap", NULL };
217 #elif defined(HAVE_LPSTAT)
218 int fd;
219 FILE *p;
220 #endif
221
222 HRP_index = 0; /* fail safe at Get_Next_HR_Print */
223
224 if (HRP_name) {
225 for (i = 0; i < HRP_nbrnames; i++)
226 free(HRP_name[i]);
227 HRP_nbrnames = 0;
228 HRP_maxnames = 0;
229 SNMP_FREE(HRP_name);
230 }
231
232 #ifdef HAVE_PRINTCAP
233 if ((p = fopen("/etc/printcap", "r")) != NULL) {
234 char buf[BUFSIZ], *ptr;
235 while (fgets(buf, sizeof buf, p)) {
236 buf[strlen(buf) - 1] = 0;
237 if (buf[0] == '#' || buf[0] == 0 || buf[0] == ' '
238 || buf[0] == '\t')
239 continue;
240 if ((ptr = strchr(buf, '\\')))
241 *ptr = 0;
242 if ((ptr = strchr(buf, ':')))
243 *ptr = 0;
244 if ((ptr = strchr(buf, '|')))
245 *ptr = 0;
246 ptr = buf;
247 #elif defined(HAVE_CGETNEXT)
248 {
249 char *buf = NULL, *ptr;
250 while (cgetnext(&buf, caps) > 0) {
251 if ((ptr = strchr(buf, ':')))
252 *ptr = 0;
253 if ((ptr = strchr(buf, '|')))
254 *ptr = 0;
255 ptr = buf;
256 #elif defined(HAVE_LPSTAT)
257 if ((p = run_lpstat(&fd)) != NULL) {
258 char buf[BUFSIZ], ptr[BUFSIZ];
259 while (fgets(buf, sizeof buf, p)) {
260 sscanf(buf, "%*s %*s %[^:]", ptr);
261 #endif
262 if (HRP_nbrnames == HRP_maxnames) {
263 char **tmp;
264 tmp = (char **) calloc(HRP_maxnames + HRP_MAX_INCR, sizeof(char *));
265 if (!tmp)
266 goto finish;
267 if (HRP_name) {
268 memcpy(tmp, HRP_name, HRP_nbrnames * sizeof(char *));
269 free(HRP_name);
270 }
271 HRP_maxnames += HRP_MAX_INCR;
272 HRP_name = tmp;
273 }
274 HRP_name[HRP_nbrnames++] = strdup(ptr);
275 #if !defined(HAVE_PRINTCAP) && defined(HAVE_CGETNEXT)
276 if (buf)
277 free(buf);
278 #endif
279 }
280 finish:
281 #if defined(HAVE_PRINTCAP)
282 fclose(p);
283 #elif defined(HAVE_CGETNEXT)
284 cgetclose();
285 #elif defined(HAVE_LPSTAT)
286 fclose(p);
287 close(fd);
288 #endif
289 }
290 #endif /* HAVE_anything */
291 }
292
293 int
294 Get_Next_HR_Print(void)
295 {
296 /*
297 * The initial implementation system
298 * has no printers attached, and I've
299 * no real idea how to detect them,
300 * so don't bother.
301 */
302 if (HRP_index < HRP_nbrnames) /* No printer */
303 return (HRDEV_PRINTER << HRDEV_TYPE_SHIFT) + HRP_index++;
304 else
305 return -1;
306 }
307
308 const char *
309 describe_printer(int idx)
310 {
311 if (HRP_index == 0) /* return empty string if not initialized */
312 return "";
313
314 DEBUGMSGTL(("host/hr_print", "describe p: %d/%d %s\n", HRP_index, idx,
315 HRP_name[HRP_index - 1]));
316 return HRP_name[HRP_index - 1];
317 }
318
319 int
320 printer_status(int idx)
321 {
322 /*
323 * hrDeviceStatus OBJECT-TYPE
324 * SYNTAX INTEGER {
325 * unknown(1), running(2), warning(3), testing(4), down(5)
326 * }
327 */
328
329 return 1; /* unknown */
330 }
331
332 int
333 printer_detail_status(int idx)
334 {
335 /*
336 * hrPrinterStatus OBJECT-TYPE
337 * SYNTAX INTEGER {
338 * other(1), unknown(2), idle(3), printing(4), warmup(5)
339 * }
340 */
341
342 return 2; /* unknown */
343 }
344
345 int
346 printer_errors(int idx)
347 {
348 return 0;
349 }
350
351 #ifdef HAVE_LPSTAT
352 /*
353 * Run the lpstat command. If compiled with EXCACHE support, this
354 * will actually cache the output for a while which helps a lot
355 * with snmpbulkwalk (in fact, it keeps the client from exiting
356 * due to timeouts).
357 */
358 FILE *
359 run_lpstat(int *fd)
360 {
361 struct extensible ex;
362
363 memset(&ex, 0, sizeof(ex));
364 ex.command = strdup(LPSTAT_PATH " -v");
365 *fd = get_exec_output(&ex);
366 free(ex.command);
367 return *fd >= 0 ? fdopen(*fd, "r") : NULL;
368 }
369 #endif