]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testsnmp.c
2 * SNMP test program for CUPS.
4 * Copyright 2008-2014 by Apple Inc.
6 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
10 * Include necessary headers...
13 #include "cups-private.h"
14 #include "snmp-private.h"
21 static void print_packet(cups_snmp_t
*packet
, void *data
);
22 static int show_oid(int fd
, const char *community
,
23 http_addr_t
*addr
, const char *s
, int walk
);
24 static void usage(void) _CUPS_NORETURN
;
28 * 'main()' - Main entry.
31 int /* O - Exit status */
32 main(int argc
, /* I - Number of command-line args */
33 char *argv
[]) /* I - Command-line arguments */
35 int i
; /* Looping var */
36 int fd
= -1; /* SNMP socket */
37 http_addrlist_t
*host
= NULL
; /* Address of host */
38 int walk
= 0; /* Walk OIDs? */
39 char *oid
= NULL
; /* Last OID shown */
40 const char *community
; /* Community name */
43 fputs("_cupsSNMPDefaultCommunity: ", stdout
);
45 if ((community
= _cupsSNMPDefaultCommunity()) == NULL
)
47 puts("FAIL (NULL community name)");
51 printf("PASS (%s)\n", community
);
54 * Query OIDs from the command-line...
57 for (i
= 1; i
< argc
; i
++)
58 if (!strcmp(argv
[i
], "-c"))
67 else if (!strcmp(argv
[i
], "-d"))
68 _cupsSNMPSetDebug(10);
69 else if (!strcmp(argv
[i
], "-w"))
73 if ((host
= httpAddrGetList(argv
[i
], AF_UNSPEC
, "161")) == NULL
)
75 printf("testsnmp: Unable to find \"%s\"!\n", argv
[1]);
81 fputs("_cupsSNMPOpen: ", stdout
);
83 if ((fd
= _cupsSNMPOpen(host
->addr
.addr
.sa_family
)) < 0)
85 printf("FAIL (%s)\n", strerror(errno
));
92 else if (!show_oid(fd
, community
, &(host
->addr
), argv
[i
], walk
))
102 if (!show_oid(fd
, community
, &(host
->addr
),
103 walk
? ".1.3.6.1.2.1.43" :
104 ".1.3.6.1.2.1.43.10.2.1.4.1.1", walk
))
113 * 'print_packet()' - Print the contents of the response packet.
117 print_packet(cups_snmp_t
*packet
, /* I - SNMP response packet */
118 void *data
) /* I - User data pointer (not used) */
120 unsigned i
; /* Looping var */
121 char temp
[1024]; /* Temporary OID string */
126 printf("%s = ", _cupsSNMPOIDToString(packet
->object_name
, temp
, sizeof(temp
)));
128 switch (packet
->object_type
)
130 case CUPS_ASN1_BOOLEAN
:
131 printf("BOOLEAN %s\n",
132 packet
->object_value
.boolean
? "TRUE" : "FALSE");
135 case CUPS_ASN1_INTEGER
:
136 printf("INTEGER %d\n", packet
->object_value
.integer
);
139 case CUPS_ASN1_BIT_STRING
:
140 printf("BIT-STRING \"%s\"\n",
141 (char *)packet
->object_value
.string
.bytes
);
144 case CUPS_ASN1_OCTET_STRING
:
145 printf("OCTET-STRING \"%s\"\n",
146 (char *)packet
->object_value
.string
.bytes
);
149 case CUPS_ASN1_NULL_VALUE
:
154 printf("OID %s\n", _cupsSNMPOIDToString(packet
->object_value
.oid
,
155 temp
, sizeof(temp
)));
158 case CUPS_ASN1_HEX_STRING
:
159 fputs("Hex-STRING", stdout
);
160 for (i
= 0; i
< packet
->object_value
.string
.num_bytes
; i
++)
161 printf(" %02X", packet
->object_value
.string
.bytes
[i
]);
165 case CUPS_ASN1_COUNTER
:
166 printf("Counter %d\n", packet
->object_value
.counter
);
169 case CUPS_ASN1_GAUGE
:
170 printf("Gauge %u\n", packet
->object_value
.gauge
);
173 case CUPS_ASN1_TIMETICKS
:
174 printf("Timeticks %u days, %u:%02u:%02u.%02u\n",
175 packet
->object_value
.timeticks
/ 8640000,
176 (packet
->object_value
.timeticks
/ 360000) % 24,
177 (packet
->object_value
.timeticks
/ 6000) % 60,
178 (packet
->object_value
.timeticks
/ 100) % 60,
179 packet
->object_value
.timeticks
% 100);
183 printf("Unknown-%X\n", packet
->object_type
);
190 * 'show_oid()' - Show the specified OID.
193 static int /* O - 1 on success, 0 on error */
194 show_oid(int fd
, /* I - SNMP socket */
195 const char *community
, /* I - Community name */
196 http_addr_t
*addr
, /* I - Address to query */
197 const char *s
, /* I - OID to query */
198 int walk
) /* I - Walk OIDs? */
200 int i
; /* Looping var */
201 int oid
[CUPS_SNMP_MAX_OID
]; /* OID */
202 cups_snmp_t packet
; /* SNMP packet */
203 char temp
[1024]; /* Temporary OID string */
206 if (!_cupsSNMPStringToOID(s
, oid
, sizeof(oid
) / sizeof(oid
[0])))
208 puts("testsnmp: Bad OID");
214 printf("_cupsSNMPWalk(%s): ", _cupsSNMPOIDToString(oid
, temp
, sizeof(temp
)));
216 if (_cupsSNMPWalk(fd
, addr
, CUPS_SNMP_VERSION_1
, community
, oid
, 5.0,
217 print_packet
, NULL
) < 0)
219 printf("FAIL (%s)\n", strerror(errno
));
225 printf("_cupsSNMPWrite(%s): ", _cupsSNMPOIDToString(oid
, temp
, sizeof(temp
)));
227 if (!_cupsSNMPWrite(fd
, addr
, CUPS_SNMP_VERSION_1
, community
,
228 CUPS_ASN1_GET_REQUEST
, 1, oid
))
230 printf("FAIL (%s)\n", strerror(errno
));
236 fputs("_cupsSNMPRead(5.0): ", stdout
);
238 if (!_cupsSNMPRead(fd
, &packet
, 5.0))
240 puts("FAIL (timeout)");
244 if (!_cupsSNMPIsOID(&packet
, oid
))
246 printf("FAIL (bad OID %d", packet
.object_name
[0]);
247 for (i
= 1; packet
.object_name
[i
] >= 0; i
++)
248 printf(".%d", packet
.object_name
[i
]);
255 printf("FAIL (%s)\n", packet
.error
);
261 print_packet(&packet
, NULL
);
269 * 'usage()' - Show program usage and exit.
275 puts("Usage: testsnmp [options] host-or-ip [oid ...]");
279 puts(" -c community Set community name");
280 puts(" -d Enable debugging");
281 puts(" -w Walk all OIDs under the specified one");