]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testsnmp.c
4 * SNMP test program for CUPS.
6 * Copyright 2008-2010 by Apple Inc.
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
14 * This file is subject to the Apple OS-Developed Software exception.
18 * main() - Main entry.
19 * scan_oid() - Scan an OID value.
20 * show_oid() - Show the specified OID.
21 * usage() - Show program usage and exit.
25 * Include necessary headers...
28 #include "cups-private.h"
29 #include "snmp-private.h"
36 static void print_packet(cups_snmp_t
*packet
, void *data
);
37 static int show_oid(int fd
, const char *community
,
38 http_addr_t
*addr
, const char *s
, int walk
);
39 static void usage(void) __attribute__((noreturn
));
43 * 'main()' - Main entry.
46 int /* O - Exit status */
47 main(int argc
, /* I - Number of command-line args */
48 char *argv
[]) /* I - Command-line arguments */
50 int i
; /* Looping var */
51 int fd
= -1; /* SNMP socket */
52 http_addrlist_t
*host
= NULL
; /* Address of host */
53 int walk
= 0; /* Walk OIDs? */
54 char *oid
= NULL
; /* Last OID shown */
55 const char *community
; /* Community name */
58 fputs("_cupsSNMPDefaultCommunity: ", stdout
);
60 if ((community
= _cupsSNMPDefaultCommunity()) == NULL
)
62 puts("FAIL (NULL community name)");
66 printf("PASS (%s)\n", community
);
69 * Query OIDs from the command-line...
72 for (i
= 1; i
< argc
; i
++)
73 if (!strcmp(argv
[i
], "-c"))
82 else if (!strcmp(argv
[i
], "-d"))
83 _cupsSNMPSetDebug(10);
84 else if (!strcmp(argv
[i
], "-w"))
88 if ((host
= httpAddrGetList(argv
[i
], AF_UNSPEC
, "161")) == NULL
)
90 printf("testsnmp: Unable to find \"%s\"!\n", argv
[1]);
96 fputs("_cupsSNMPOpen: ", stdout
);
98 if ((fd
= _cupsSNMPOpen(host
->addr
.addr
.sa_family
)) < 0)
100 printf("FAIL (%s)\n", strerror(errno
));
107 else if (!show_oid(fd
, community
, &(host
->addr
), argv
[i
], walk
))
117 if (!show_oid(fd
, community
, &(host
->addr
),
118 walk
? ".1.3.6.1.2.1.43" :
119 ".1.3.6.1.2.1.43.10.2.1.4.1.1", walk
))
128 * 'print_packet()' - Print the contents of the response packet.
132 print_packet(cups_snmp_t
*packet
, /* I - SNMP response packet */
133 void *data
) /* I - User data pointer (not used) */
135 int i
; /* Looping var */
136 char temp
[1024]; /* Temporary OID string */
141 printf("%s = ", _cupsSNMPOIDToString(packet
->object_name
, temp
, sizeof(temp
)));
143 switch (packet
->object_type
)
145 case CUPS_ASN1_BOOLEAN
:
146 printf("BOOLEAN %s\n",
147 packet
->object_value
.boolean
? "TRUE" : "FALSE");
150 case CUPS_ASN1_INTEGER
:
151 printf("INTEGER %d\n", packet
->object_value
.integer
);
154 case CUPS_ASN1_BIT_STRING
:
155 printf("BIT-STRING \"%s\"\n",
156 (char *)packet
->object_value
.string
.bytes
);
159 case CUPS_ASN1_OCTET_STRING
:
160 printf("OCTET-STRING \"%s\"\n",
161 (char *)packet
->object_value
.string
.bytes
);
164 case CUPS_ASN1_NULL_VALUE
:
169 printf("OID %s\n", _cupsSNMPOIDToString(packet
->object_value
.oid
,
170 temp
, sizeof(temp
)));
173 case CUPS_ASN1_HEX_STRING
:
174 fputs("Hex-STRING", stdout
);
175 for (i
= 0; i
< packet
->object_value
.string
.num_bytes
; i
++)
176 printf(" %02X", packet
->object_value
.string
.bytes
[i
]);
180 case CUPS_ASN1_COUNTER
:
181 printf("Counter %d\n", packet
->object_value
.counter
);
184 case CUPS_ASN1_GAUGE
:
185 printf("Gauge %u\n", packet
->object_value
.gauge
);
188 case CUPS_ASN1_TIMETICKS
:
189 printf("Timeticks %u days, %u:%02u:%02u.%02u\n",
190 packet
->object_value
.timeticks
/ 8640000,
191 (packet
->object_value
.timeticks
/ 360000) % 24,
192 (packet
->object_value
.timeticks
/ 6000) % 60,
193 (packet
->object_value
.timeticks
/ 100) % 60,
194 packet
->object_value
.timeticks
% 100);
198 printf("Unknown-%X\n", packet
->object_type
);
205 * 'show_oid()' - Show the specified OID.
208 static int /* O - 1 on success, 0 on error */
209 show_oid(int fd
, /* I - SNMP socket */
210 const char *community
, /* I - Community name */
211 http_addr_t
*addr
, /* I - Address to query */
212 const char *s
, /* I - OID to query */
213 int walk
) /* I - Walk OIDs? */
215 int i
; /* Looping var */
216 int oid
[CUPS_SNMP_MAX_OID
]; /* OID */
217 cups_snmp_t packet
; /* SNMP packet */
218 char temp
[1024]; /* Temporary OID string */
221 if (!_cupsSNMPStringToOID(s
, oid
, sizeof(oid
) / sizeof(oid
[0])))
223 puts("testsnmp: Bad OID");
229 printf("_cupsSNMPWalk(%s): ", _cupsSNMPOIDToString(oid
, temp
, sizeof(temp
)));
231 if (_cupsSNMPWalk(fd
, addr
, CUPS_SNMP_VERSION_1
, community
, oid
, 5.0,
232 print_packet
, NULL
) < 0)
234 printf("FAIL (%s)\n", strerror(errno
));
240 printf("_cupsSNMPWrite(%s): ", _cupsSNMPOIDToString(oid
, temp
, sizeof(temp
)));
242 if (!_cupsSNMPWrite(fd
, addr
, CUPS_SNMP_VERSION_1
, community
,
243 CUPS_ASN1_GET_REQUEST
, 1, oid
))
245 printf("FAIL (%s)\n", strerror(errno
));
251 fputs("_cupsSNMPRead(5.0): ", stdout
);
253 if (!_cupsSNMPRead(fd
, &packet
, 5.0))
255 puts("FAIL (timeout)");
259 if (!_cupsSNMPIsOID(&packet
, oid
))
261 printf("FAIL (bad OID %d", packet
.object_name
[0]);
262 for (i
= 1; packet
.object_name
[i
] >= 0; i
++)
263 printf(".%d", packet
.object_name
[i
]);
270 printf("FAIL (%s)\n", packet
.error
);
276 print_packet(&packet
, NULL
);
284 * 'usage()' - Show program usage and exit.
290 puts("Usage: testsnmp [options] host-or-ip [oid ...]");
294 puts(" -c community Set community name");
295 puts(" -d Enable debugging");
296 puts(" -w Walk all OIDs under the specified one");