]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testsnmp.c
4 * SNMP test program for the Common UNIX Printing System (CUPS).
6 * Copyright 2008 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...
32 #include "snmp-private.h"
39 static void print_packet(cups_snmp_t
*packet
, void *data
);
40 static int show_oid(int fd
, const char *community
,
41 http_addr_t
*addr
, const char *s
, int walk
);
42 static void usage(void);
46 * 'main()' - Main entry.
49 int /* O - Exit status */
50 main(int argc
, /* I - Number of command-line args */
51 char *argv
[]) /* I - Command-line arguments */
53 int i
; /* Looping var */
54 int fd
= -1; /* SNMP socket */
55 http_addrlist_t
*host
= NULL
; /* Address of host */
56 int walk
= 0; /* Walk OIDs? */
57 char *oid
= NULL
; /* Last OID shown */
58 const char *community
; /* Community name */
61 fputs("_cupsSNMPDefaultCommunity: ", stdout
);
63 if ((community
= _cupsSNMPDefaultCommunity()) == NULL
)
65 puts("FAIL (NULL community name)");
69 printf("PASS (%s)\n", community
);
72 * Query OIDs from the command-line...
75 for (i
= 1; i
< argc
; i
++)
76 if (!strcmp(argv
[i
], "-c"))
85 else if (!strcmp(argv
[i
], "-d"))
86 _cupsSNMPSetDebug(10);
87 else if (!strcmp(argv
[i
], "-w"))
91 if ((host
= httpAddrGetList(argv
[i
], AF_UNSPEC
, "161")) == NULL
)
93 printf("testsnmp: Unable to find \"%s\"!\n", argv
[1]);
99 fputs("_cupsSNMPOpen: ", stdout
);
101 if ((fd
= _cupsSNMPOpen(host
->addr
.addr
.sa_family
)) < 0)
103 printf("FAIL (%s)\n", strerror(errno
));
110 else if (!show_oid(fd
, community
, &(host
->addr
), argv
[i
], walk
))
120 if (!show_oid(fd
, community
, &(host
->addr
),
121 walk
? ".1.3.6.1.2.1.43" :
122 ".1.3.6.1.2.1.43.10.2.1.4.1.1", walk
))
131 * 'print_packet()' - Print the contents of the response packet.
135 print_packet(cups_snmp_t
*packet
, /* I - SNMP response packet */
136 void *data
) /* I - User data pointer (not used) */
138 int i
; /* Looping var */
139 char temp
[1024]; /* Temporary OID string */
144 printf("%s = ", _cupsSNMPOIDToString(packet
->object_name
, temp
, sizeof(temp
)));
146 switch (packet
->object_type
)
148 case CUPS_ASN1_BOOLEAN
:
149 printf("BOOLEAN %s\n",
150 packet
->object_value
.boolean
? "TRUE" : "FALSE");
153 case CUPS_ASN1_INTEGER
:
154 printf("INTEGER %d\n", packet
->object_value
.integer
);
157 case CUPS_ASN1_BIT_STRING
:
158 printf("BIT-STRING \"%s\"\n", packet
->object_value
.string
);
161 case CUPS_ASN1_OCTET_STRING
:
162 printf("OCTET-STRING \"%s\"\n", packet
->object_value
.string
);
165 case CUPS_ASN1_NULL_VALUE
:
170 printf("OID %s\n", _cupsSNMPOIDToString(packet
->object_value
.oid
,
171 temp
, sizeof(temp
)));
174 case CUPS_ASN1_HEX_STRING
:
175 fputs("Hex-STRING", stdout
);
176 for (i
= 0; i
< packet
->object_value
.hex_string
.num_bytes
; i
++)
177 printf(" %02X", packet
->object_value
.hex_string
.bytes
[i
]);
181 case CUPS_ASN1_COUNTER
:
182 printf("Counter %d\n", packet
->object_value
.counter
);
185 case CUPS_ASN1_GAUGE
:
186 printf("Gauge %u\n", packet
->object_value
.gauge
);
189 case CUPS_ASN1_TIMETICKS
:
190 printf("Timeticks %u days, %u:%02u:%02u.%02u\n",
191 packet
->object_value
.timeticks
/ 8640000,
192 (packet
->object_value
.timeticks
/ 360000) % 24,
193 (packet
->object_value
.timeticks
/ 6000) % 60,
194 (packet
->object_value
.timeticks
/ 100) % 60,
195 packet
->object_value
.timeticks
% 100);
199 printf("Unknown-%X\n", packet
->object_type
);
206 * 'show_oid()' - Show the specified OID.
209 static int /* O - 1 on success, 0 on error */
210 show_oid(int fd
, /* I - SNMP socket */
211 const char *community
, /* I - Community name */
212 http_addr_t
*addr
, /* I - Address to query */
213 const char *s
, /* I - OID to query */
214 int walk
) /* I - Walk OIDs? */
216 int i
; /* Looping var */
217 int oid
[CUPS_SNMP_MAX_OID
]; /* OID */
218 cups_snmp_t packet
; /* SNMP packet */
219 char temp
[1024]; /* Temporary OID string */
222 if (!_cupsSNMPStringToOID(s
, oid
, sizeof(oid
) / sizeof(oid
[0])))
224 puts("testsnmp: Bad OID");
230 printf("_cupsSNMPWalk(%s): ", _cupsSNMPOIDToString(oid
, temp
, sizeof(temp
)));
232 if (_cupsSNMPWalk(fd
, addr
, CUPS_SNMP_VERSION_1
, community
, oid
, 5.0,
233 print_packet
, NULL
) < 0)
235 printf("FAIL (%s)\n", strerror(errno
));
241 printf("_cupsSNMPWrite(%s): ", _cupsSNMPOIDToString(oid
, temp
, sizeof(temp
)));
243 if (!_cupsSNMPWrite(fd
, addr
, CUPS_SNMP_VERSION_1
, community
,
244 CUPS_ASN1_GET_REQUEST
, 1, oid
))
246 printf("FAIL (%s)\n", strerror(errno
));
252 fputs("_cupsSNMPRead(5.0): ", stdout
);
254 if (!_cupsSNMPRead(fd
, &packet
, 5.0))
256 puts("FAIL (timeout)");
260 if (!_cupsSNMPIsOID(&packet
, oid
))
262 printf("FAIL (bad OID %d", packet
.object_name
[0]);
263 for (i
= 1; packet
.object_name
[i
] >= 0; i
++)
264 printf(".%d", packet
.object_name
[i
]);
271 printf("FAIL (%s)\n", packet
.error
);
277 print_packet(&packet
, NULL
);
285 * 'usage()' - Show program usage and exit.
291 puts("Usage: testsnmp [options] host-or-ip [oid ...]");
295 puts(" -c community Set community name");
296 puts(" -d Enable debugging");
297 puts(" -w Walk all OIDs under the specified one");