check_hpjd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /******************************************************************************
  2. *
  3. * CHECK_HPJD.C
  4. *
  5. * Program: HP printer plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  8. *
  9. * Last Modified: $Date$
  10. *
  11. * Command line: CHECK_HPJD <ip_address> [community]
  12. *
  13. * Description:
  14. *
  15. * This plugin will attempt to check the status of an HP printer. The
  16. * printer must have a JetDirect card installed and TCP/IP protocol
  17. * stack enabled. This plugin has only been tested on a few printers
  18. * and may not work well on all models of JetDirect cards. Multiple
  19. * port JetDirect devices must have an IP address assigned to each
  20. * port in order to be monitored.
  21. *
  22. * Dependencies:
  23. *
  24. * This plugin used the 'snmpget' command included with the UCD-SNMP
  25. * package. If you don't have the package installed you will need to
  26. * download it from http://ucd-snmp.ucdavis.edu before you can use
  27. * this plugin.
  28. *
  29. * Return Values:
  30. *
  31. * UNKNOWN = The plugin could not read/process the output from the printer
  32. * OK = Printer looks normal
  33. * WARNING = Low toner, paper jam, intervention required, paper out, etc.
  34. * CRITICAL = The printer could not be reached (it's probably turned off)
  35. *
  36. * Acknowledgements:
  37. *
  38. * The idea for the plugin (as well as some code) were taken from Jim
  39. * Trocki's pinter alert script in his "mon" utility, found at
  40. * http://www.kernel.org/software/mon
  41. *
  42. * Notes:
  43. * 'JetDirect' is copyrighted by Hewlett-Packard.
  44. * HP, please don't sue me... :-)
  45. *
  46. * License Information:
  47. *
  48. * This program is free software; you can redistribute it and/or modify
  49. * it under the terms of the GNU General Public License as published by
  50. * the Free Software Foundation; either version 2 of the License, or
  51. * (at your option) any later version.
  52. *
  53. * This program is distributed in the hope that it will be useful,
  54. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  55. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  56. * GNU General Public License for more details.
  57. *
  58. * You should have received a copy of the GNU General Public License
  59. * along with this program; if not, write to the Free Software
  60. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  61. *
  62. *****************************************************************************/
  63. #include "common.h"
  64. #include "popen.h"
  65. #include "utils.h"
  66. #define PROGNAME "check_hpjd"
  67. #define HPJD_LINE_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
  68. #define HPJD_PAPER_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
  69. #define HPJD_INTERVENTION_REQUIRED ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
  70. #define HPJD_GD_PERIPHERAL_ERROR ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
  71. #define HPJD_GD_PAPER_JAM ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
  72. #define HPJD_GD_PAPER_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
  73. #define HPJD_GD_TONER_LOW ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
  74. #define HPJD_GD_PAGE_PUNT ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
  75. #define HPJD_GD_MEMORY_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
  76. #define HPJD_GD_DOOR_OPEN ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
  77. #define HPJD_GD_PAPER_OUTPUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
  78. #define HPJD_GD_STATUS_DISPLAY ".1.3.6.1.4.1.11.2.3.9.1.1.3"
  79. #define ONLINE 0
  80. #define OFFLINE 1
  81. int process_arguments (int, char **);
  82. int call_getopt (int, char **);
  83. int validate_arguments (void);
  84. void print_help (void);
  85. void print_usage (void);
  86. char *community = NULL;
  87. char *address = NULL;
  88. int
  89. main (int argc, char **argv)
  90. {
  91. char command_line[1024];
  92. int result;
  93. int line;
  94. char input_buffer[MAX_INPUT_BUFFER];
  95. char query_string[512];
  96. char error_message[MAX_INPUT_BUFFER];
  97. char *temp_buffer;
  98. int line_status = ONLINE;
  99. int paper_status = 0;
  100. int intervention_required = 0;
  101. int peripheral_error = 0;
  102. int paper_jam = 0;
  103. int paper_out = 0;
  104. int toner_low = 0;
  105. int page_punt = 0;
  106. int memory_out = 0;
  107. int door_open = 0;
  108. int paper_output = 0;
  109. char display_message[MAX_INPUT_BUFFER];
  110. if (process_arguments (argc, argv) != OK)
  111. usage ("Invalid command arguments supplied\n");
  112. /* removed ' 2>1' at end of command 10/27/1999 - EG */
  113. /* create the query string */
  114. sprintf
  115. (query_string,
  116. "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0",
  117. HPJD_LINE_STATUS,
  118. HPJD_PAPER_STATUS,
  119. HPJD_INTERVENTION_REQUIRED,
  120. HPJD_GD_PERIPHERAL_ERROR,
  121. HPJD_GD_PAPER_JAM,
  122. HPJD_GD_PAPER_OUT,
  123. HPJD_GD_TONER_LOW,
  124. HPJD_GD_PAGE_PUNT,
  125. HPJD_GD_MEMORY_OUT,
  126. HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);
  127. /* get the command to run */
  128. sprintf (command_line, "%s -v 1 %s %s %s", PATH_TO_SNMPGET, address,
  129. community, query_string);
  130. /* run the command */
  131. child_process = spopen (command_line);
  132. if (child_process == NULL) {
  133. printf ("Could not open pipe: %s\n", command_line);
  134. return STATE_UNKNOWN;
  135. }
  136. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  137. if (child_stderr == NULL) {
  138. printf ("Could not open stderr for %s\n", command_line);
  139. }
  140. result = STATE_OK;
  141. line = 0;
  142. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  143. /* strip the newline character from the end of the input */
  144. if (input_buffer[strlen (input_buffer) - 1] == '\n')
  145. input_buffer[strlen (input_buffer) - 1] = 0;
  146. line++;
  147. temp_buffer = strtok (input_buffer, "=");
  148. temp_buffer = strtok (NULL, "=");
  149. switch (line) {
  150. case 1: /* 1st line should contain the line status */
  151. if (temp_buffer != NULL)
  152. line_status = atoi (temp_buffer);
  153. else {
  154. result = STATE_UNKNOWN;
  155. strcpy (error_message, input_buffer);
  156. }
  157. break;
  158. case 2: /* 2nd line should contain the paper status */
  159. if (temp_buffer != NULL)
  160. paper_status = atoi (temp_buffer);
  161. else {
  162. result = STATE_UNKNOWN;
  163. strcpy (error_message, input_buffer);
  164. }
  165. break;
  166. case 3: /* 3rd line should be intervention required */
  167. if (temp_buffer != NULL)
  168. intervention_required = atoi (temp_buffer);
  169. else {
  170. result = STATE_UNKNOWN;
  171. strcpy (error_message, input_buffer);
  172. }
  173. break;
  174. case 4: /* 4th line should be peripheral error */
  175. if (temp_buffer != NULL)
  176. peripheral_error = atoi (temp_buffer);
  177. else {
  178. result = STATE_UNKNOWN;
  179. strcpy (error_message, input_buffer);
  180. }
  181. break;
  182. case 5: /* 5th line should contain the paper jam status */
  183. if (temp_buffer != NULL)
  184. paper_jam = atoi (temp_buffer);
  185. else {
  186. result = STATE_UNKNOWN;
  187. strcpy (error_message, input_buffer);
  188. }
  189. break;
  190. case 6: /* 6th line should contain the paper out status */
  191. if (temp_buffer != NULL)
  192. paper_out = atoi (temp_buffer);
  193. else {
  194. result = STATE_UNKNOWN;
  195. strcpy (error_message, input_buffer);
  196. }
  197. break;
  198. case 7: /* 7th line should contain the toner low status */
  199. if (temp_buffer != NULL)
  200. toner_low = atoi (temp_buffer);
  201. else {
  202. result = STATE_UNKNOWN;
  203. strcpy (error_message, input_buffer);
  204. }
  205. break;
  206. case 8: /* did data come too slow for engine */
  207. if (temp_buffer != NULL)
  208. page_punt = atoi (temp_buffer);
  209. else {
  210. result = STATE_UNKNOWN;
  211. strcpy (error_message, input_buffer);
  212. }
  213. break;
  214. case 9: /* did we run out of memory */
  215. if (temp_buffer != NULL)
  216. memory_out = atoi (temp_buffer);
  217. else {
  218. result = STATE_UNKNOWN;
  219. strcpy (error_message, input_buffer);
  220. }
  221. break;
  222. case 10: /* is there a door open */
  223. if (temp_buffer != NULL)
  224. door_open = atoi (temp_buffer);
  225. else {
  226. result = STATE_UNKNOWN;
  227. strcpy (error_message, input_buffer);
  228. }
  229. break;
  230. case 11: /* is output tray full */
  231. if (temp_buffer != NULL)
  232. paper_output = atoi (temp_buffer);
  233. else {
  234. result = STATE_UNKNOWN;
  235. strcpy (error_message, input_buffer);
  236. }
  237. break;
  238. case 12: /* display panel message */
  239. if (temp_buffer != NULL)
  240. strcpy (display_message, temp_buffer + 1);
  241. else {
  242. result = STATE_UNKNOWN;
  243. strcpy (error_message, input_buffer);
  244. }
  245. break;
  246. default:
  247. break;
  248. }
  249. /* break out of the read loop if we encounter an error */
  250. if (result != STATE_OK)
  251. break;
  252. }
  253. /* WARNING if output found on stderr */
  254. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  255. result = max (result, STATE_WARNING);
  256. /* close stderr */
  257. (void) fclose (child_stderr);
  258. /* close the pipe */
  259. if (spclose (child_process))
  260. result = max (result, STATE_WARNING);
  261. /* if there wasn't any output, display an error */
  262. if (line == 0) {
  263. /*
  264. result=STATE_UNKNOWN;
  265. strcpy(error_message,"Error: Could not read plugin output\n");
  266. */
  267. /* might not be the problem, but most likely is.. */
  268. result = STATE_UNKNOWN;
  269. sprintf (error_message, "Timeout: No response from %s\n", address);
  270. }
  271. /* if we had no read errors, check the printer status results... */
  272. if (result == STATE_OK) {
  273. if (paper_jam) {
  274. result = STATE_WARNING;
  275. strcpy (error_message, "Paper Jam");
  276. }
  277. else if (paper_out) {
  278. result = STATE_WARNING;
  279. strcpy (error_message, "Out of Paper");
  280. }
  281. else if (line_status == OFFLINE) {
  282. if (strcmp (error_message, "POWERSAVE ON") != 0) {
  283. result = STATE_WARNING;
  284. strcpy (error_message, "Printer Offline");
  285. }
  286. }
  287. else if (peripheral_error) {
  288. result = STATE_WARNING;
  289. strcpy (error_message, "Peripheral Error");
  290. }
  291. else if (intervention_required) {
  292. result = STATE_WARNING;
  293. strcpy (error_message, "Intervention Required");
  294. }
  295. else if (toner_low) {
  296. result = STATE_WARNING;
  297. strcpy (error_message, "Toner Low");
  298. }
  299. else if (memory_out) {
  300. result = STATE_WARNING;
  301. strcpy (error_message, "Insufficient Memory");
  302. }
  303. else if (door_open) {
  304. result = STATE_WARNING;
  305. strcpy (error_message, "A Door is Open");
  306. }
  307. else if (paper_output) {
  308. result = STATE_WARNING;
  309. strcpy (error_message, "Output Tray is Full");
  310. }
  311. else if (page_punt) {
  312. result = STATE_WARNING;
  313. strcpy (error_message, "Data too Slow for Engine");
  314. }
  315. else if (paper_status) {
  316. result = STATE_WARNING;
  317. strcpy (error_message, "Unknown Paper Error");
  318. }
  319. }
  320. if (result == STATE_OK)
  321. printf ("Printer ok - (%s)\n", display_message);
  322. else if (result == STATE_UNKNOWN) {
  323. printf ("%s\n", error_message);
  324. /* if printer could not be reached, escalate to critical */
  325. if (strstr (error_message, "Timeout"))
  326. result = STATE_CRITICAL;
  327. }
  328. else if (result == STATE_WARNING)
  329. printf ("%s (%s)\n", error_message, display_message);
  330. return result;
  331. }
  332. /* process command-line arguments */
  333. int
  334. process_arguments (int argc, char **argv)
  335. {
  336. int c;
  337. if (argc < 2)
  338. return ERROR;
  339. for (c = 1; c < argc; c++) {
  340. if (strcmp ("-to", argv[c]) == 0)
  341. strcpy (argv[c], "-t");
  342. else if (strcmp ("-wt", argv[c]) == 0)
  343. strcpy (argv[c], "-w");
  344. else if (strcmp ("-ct", argv[c]) == 0)
  345. strcpy (argv[c], "-c");
  346. }
  347. c = 0;
  348. while ((c += (call_getopt (argc - c, &argv[c]))) < argc) {
  349. if (is_option (argv[c]))
  350. continue;
  351. if (address == NULL) {
  352. if (is_host (argv[c])) {
  353. address = argv[c];
  354. }
  355. else {
  356. usage ("Invalid host name");
  357. }
  358. }
  359. else if (community == NULL) {
  360. community = argv[c];
  361. }
  362. }
  363. if (address == NULL)
  364. address = strscpy (NULL, "127.0.0.1");
  365. return validate_arguments ();
  366. }
  367. int
  368. call_getopt (int argc, char **argv)
  369. {
  370. int c, i = 0;
  371. #ifdef HAVE_GETOPT_H
  372. int option_index = 0;
  373. static struct option long_options[] = {
  374. {"hostname", required_argument, 0, 'H'},
  375. {"expect", required_argument, 0, 'e'},
  376. /* {"critical", required_argument,0,'c'}, */
  377. /* {"warning", required_argument,0,'w'}, */
  378. /* {"port", required_argument,0,'P'}, */
  379. {"verbose", no_argument, 0, 'v'},
  380. {"version", no_argument, 0, 'V'},
  381. {"help", no_argument, 0, 'h'},
  382. {0, 0, 0, 0}
  383. };
  384. #endif
  385. while (1) {
  386. #ifdef HAVE_GETOPT_H
  387. c = getopt_long (argc, argv, "+hVH:C:", long_options, &option_index);
  388. #else
  389. c = getopt (argc, argv, "+?hVH:C:");
  390. #endif
  391. i++;
  392. if (c == -1 || c == EOF || c == 1)
  393. break;
  394. switch (c) {
  395. case 'H':
  396. case 'C':
  397. i++;
  398. }
  399. switch (c) {
  400. case 'H': /* hostname */
  401. if (is_host (optarg)) {
  402. address = optarg;
  403. }
  404. else {
  405. usage ("Invalid host name\n");
  406. }
  407. break;
  408. case 'C': /* community */
  409. community = optarg;
  410. break;
  411. case 'V': /* version */
  412. print_revision (PROGNAME, "$Revision$");
  413. exit (STATE_OK);
  414. case 'h': /* help */
  415. print_help ();
  416. exit (STATE_OK);
  417. case '?': /* help */
  418. usage ("Invalid argument\n");
  419. }
  420. }
  421. return i;
  422. }
  423. int
  424. validate_arguments (void)
  425. {
  426. return OK;
  427. }
  428. void
  429. print_help (void)
  430. {
  431. print_revision (PROGNAME, "$Revision$");
  432. printf
  433. ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
  434. "This plugin tests the STATUS of an HP printer with a JetDirect card.\n"
  435. "Ucd-snmp must be installed on the computer running the plugin.\n\n");
  436. print_usage ();
  437. printf
  438. ("\nOptions:\n"
  439. " -H, --hostname=STRING or IPADDRESS\n"
  440. " Check server on the indicated host\n"
  441. " -C, --community=STRING\n"
  442. " The SNMP community name\n"
  443. " -h, --help\n"
  444. " Print detailed help screen\n"
  445. " -V, --version\n" " Print version information\n\n");
  446. support ();
  447. }
  448. void
  449. print_usage (void)
  450. {
  451. printf
  452. ("Usage: %s -H host [-C community]\n"
  453. " %s --help\n"
  454. " %s --version\n", PROGNAME, PROGNAME, PROGNAME);
  455. }
  456. /*
  457. if(argc<2||argc>3){
  458. printf("Incorrect number of arguments supplied\n");
  459. printf("\n");
  460. print_revision(argv[0],"$Revision$");
  461. printf("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n");
  462. printf("License: GPL\n");
  463. printf("\n");
  464. printf("Usage: %s <ip_address> [community]\n",argv[0]);
  465. printf("\n");
  466. printf("Note:\n");
  467. printf(" <ip_address> = The IP address of the JetDirect card\n");
  468. printf(" [community] = An optional community string used for SNMP communication\n");
  469. printf(" with the JetDirect card. The default is 'public'.\n");
  470. printf("\n");
  471. return STATE_UNKNOWN;
  472. }
  473. // get the IP address of the JetDirect device
  474. strcpy(address,argv[1]);
  475. // get the community name to use for SNMP communication
  476. if(argc>=3)
  477. strcpy(community,argv[2]);
  478. else
  479. strcpy(community,"public");
  480. */