check_hpjd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 REVISION "$Revision$"
  68. #define COPYRIGHT "2000-2002"
  69. #define HPJD_LINE_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
  70. #define HPJD_PAPER_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
  71. #define HPJD_INTERVENTION_REQUIRED ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
  72. #define HPJD_GD_PERIPHERAL_ERROR ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
  73. #define HPJD_GD_PAPER_JAM ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
  74. #define HPJD_GD_PAPER_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
  75. #define HPJD_GD_TONER_LOW ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
  76. #define HPJD_GD_PAGE_PUNT ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
  77. #define HPJD_GD_MEMORY_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
  78. #define HPJD_GD_DOOR_OPEN ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
  79. #define HPJD_GD_PAPER_OUTPUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
  80. #define HPJD_GD_STATUS_DISPLAY ".1.3.6.1.4.1.11.2.3.9.1.1.3"
  81. #define ONLINE 0
  82. #define OFFLINE 1
  83. int process_arguments (int, char **);
  84. int validate_arguments (void);
  85. void print_help (void);
  86. void print_usage (void);
  87. char *community = NULL;
  88. char *address = "127.0.0.1";
  89. int
  90. main (int argc, char **argv)
  91. {
  92. char command_line[1024];
  93. int result;
  94. int line;
  95. char input_buffer[MAX_INPUT_BUFFER];
  96. char query_string[512];
  97. char error_message[MAX_INPUT_BUFFER];
  98. char *temp_buffer;
  99. int line_status = ONLINE;
  100. int paper_status = 0;
  101. int intervention_required = 0;
  102. int peripheral_error = 0;
  103. int paper_jam = 0;
  104. int paper_out = 0;
  105. int toner_low = 0;
  106. int page_punt = 0;
  107. int memory_out = 0;
  108. int door_open = 0;
  109. int paper_output = 0;
  110. char display_message[MAX_INPUT_BUFFER];
  111. if (process_arguments (argc, argv) != OK)
  112. usage ("Invalid command arguments supplied\n");
  113. /* removed ' 2>1' at end of command 10/27/1999 - EG */
  114. /* create the query string */
  115. sprintf
  116. (query_string,
  117. "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0",
  118. HPJD_LINE_STATUS,
  119. HPJD_PAPER_STATUS,
  120. HPJD_INTERVENTION_REQUIRED,
  121. HPJD_GD_PERIPHERAL_ERROR,
  122. HPJD_GD_PAPER_JAM,
  123. HPJD_GD_PAPER_OUT,
  124. HPJD_GD_TONER_LOW,
  125. HPJD_GD_PAGE_PUNT,
  126. HPJD_GD_MEMORY_OUT,
  127. HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);
  128. /* get the command to run */
  129. sprintf (command_line, "%s -m : -v 1 %s -c %s %s", PATH_TO_SNMPGET, address,
  130. community, query_string);
  131. /* run the command */
  132. child_process = spopen (command_line);
  133. if (child_process == NULL) {
  134. printf ("Could not open pipe: %s\n", command_line);
  135. return STATE_UNKNOWN;
  136. }
  137. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  138. if (child_stderr == NULL) {
  139. printf ("Could not open stderr for %s\n", command_line);
  140. }
  141. result = STATE_OK;
  142. line = 0;
  143. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  144. /* strip the newline character from the end of the input */
  145. if (input_buffer[strlen (input_buffer) - 1] == '\n')
  146. input_buffer[strlen (input_buffer) - 1] = 0;
  147. line++;
  148. temp_buffer = strtok (input_buffer, "=");
  149. temp_buffer = strtok (NULL, "=");
  150. switch (line) {
  151. case 1: /* 1st line should contain the line status */
  152. if (temp_buffer != NULL)
  153. line_status = atoi (temp_buffer);
  154. else {
  155. result = STATE_UNKNOWN;
  156. strcpy (error_message, input_buffer);
  157. }
  158. break;
  159. case 2: /* 2nd line should contain the paper status */
  160. if (temp_buffer != NULL)
  161. paper_status = atoi (temp_buffer);
  162. else {
  163. result = STATE_UNKNOWN;
  164. strcpy (error_message, input_buffer);
  165. }
  166. break;
  167. case 3: /* 3rd line should be intervention required */
  168. if (temp_buffer != NULL)
  169. intervention_required = atoi (temp_buffer);
  170. else {
  171. result = STATE_UNKNOWN;
  172. strcpy (error_message, input_buffer);
  173. }
  174. break;
  175. case 4: /* 4th line should be peripheral error */
  176. if (temp_buffer != NULL)
  177. peripheral_error = atoi (temp_buffer);
  178. else {
  179. result = STATE_UNKNOWN;
  180. strcpy (error_message, input_buffer);
  181. }
  182. break;
  183. case 5: /* 5th line should contain the paper jam status */
  184. if (temp_buffer != NULL)
  185. paper_jam = atoi (temp_buffer);
  186. else {
  187. result = STATE_UNKNOWN;
  188. strcpy (error_message, input_buffer);
  189. }
  190. break;
  191. case 6: /* 6th line should contain the paper out status */
  192. if (temp_buffer != NULL)
  193. paper_out = atoi (temp_buffer);
  194. else {
  195. result = STATE_UNKNOWN;
  196. strcpy (error_message, input_buffer);
  197. }
  198. break;
  199. case 7: /* 7th line should contain the toner low status */
  200. if (temp_buffer != NULL)
  201. toner_low = atoi (temp_buffer);
  202. else {
  203. result = STATE_UNKNOWN;
  204. strcpy (error_message, input_buffer);
  205. }
  206. break;
  207. case 8: /* did data come too slow for engine */
  208. if (temp_buffer != NULL)
  209. page_punt = atoi (temp_buffer);
  210. else {
  211. result = STATE_UNKNOWN;
  212. strcpy (error_message, input_buffer);
  213. }
  214. break;
  215. case 9: /* did we run out of memory */
  216. if (temp_buffer != NULL)
  217. memory_out = atoi (temp_buffer);
  218. else {
  219. result = STATE_UNKNOWN;
  220. strcpy (error_message, input_buffer);
  221. }
  222. break;
  223. case 10: /* is there a door open */
  224. if (temp_buffer != NULL)
  225. door_open = atoi (temp_buffer);
  226. else {
  227. result = STATE_UNKNOWN;
  228. strcpy (error_message, input_buffer);
  229. }
  230. break;
  231. case 11: /* is output tray full */
  232. if (temp_buffer != NULL)
  233. paper_output = atoi (temp_buffer);
  234. else {
  235. result = STATE_UNKNOWN;
  236. strcpy (error_message, input_buffer);
  237. }
  238. break;
  239. case 12: /* display panel message */
  240. if (temp_buffer != NULL)
  241. strcpy (display_message, temp_buffer + 1);
  242. else {
  243. result = STATE_UNKNOWN;
  244. strcpy (error_message, input_buffer);
  245. }
  246. break;
  247. default:
  248. break;
  249. }
  250. /* break out of the read loop if we encounter an error */
  251. if (result != STATE_OK)
  252. break;
  253. }
  254. /* WARNING if output found on stderr */
  255. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  256. result = max_state (result, STATE_WARNING);
  257. /* close stderr */
  258. (void) fclose (child_stderr);
  259. /* close the pipe */
  260. if (spclose (child_process))
  261. result = max_state (result, STATE_WARNING);
  262. /* if there wasn't any output, display an error */
  263. if (line == 0) {
  264. /*
  265. result=STATE_UNKNOWN;
  266. strcpy(error_message,"Error: Could not read plugin output\n");
  267. */
  268. /* might not be the problem, but most likely is.. */
  269. result = STATE_UNKNOWN;
  270. sprintf (error_message, "Timeout: No response from %s\n", address);
  271. }
  272. /* if we had no read errors, check the printer status results... */
  273. if (result == STATE_OK) {
  274. if (paper_jam) {
  275. result = STATE_WARNING;
  276. strcpy (error_message, "Paper Jam");
  277. }
  278. else if (paper_out) {
  279. result = STATE_WARNING;
  280. strcpy (error_message, "Out of Paper");
  281. }
  282. else if (line_status == OFFLINE) {
  283. if (strcmp (error_message, "POWERSAVE ON") != 0) {
  284. result = STATE_WARNING;
  285. strcpy (error_message, "Printer Offline");
  286. }
  287. }
  288. else if (peripheral_error) {
  289. result = STATE_WARNING;
  290. strcpy (error_message, "Peripheral Error");
  291. }
  292. else if (intervention_required) {
  293. result = STATE_WARNING;
  294. strcpy (error_message, "Intervention Required");
  295. }
  296. else if (toner_low) {
  297. result = STATE_WARNING;
  298. strcpy (error_message, "Toner Low");
  299. }
  300. else if (memory_out) {
  301. result = STATE_WARNING;
  302. strcpy (error_message, "Insufficient Memory");
  303. }
  304. else if (door_open) {
  305. result = STATE_WARNING;
  306. strcpy (error_message, "A Door is Open");
  307. }
  308. else if (paper_output) {
  309. result = STATE_WARNING;
  310. strcpy (error_message, "Output Tray is Full");
  311. }
  312. else if (page_punt) {
  313. result = STATE_WARNING;
  314. strcpy (error_message, "Data too Slow for Engine");
  315. }
  316. else if (paper_status) {
  317. result = STATE_WARNING;
  318. strcpy (error_message, "Unknown Paper Error");
  319. }
  320. }
  321. if (result == STATE_OK)
  322. printf ("Printer ok - (%s)\n", display_message);
  323. else if (result == STATE_UNKNOWN) {
  324. printf ("%s\n", error_message);
  325. /* if printer could not be reached, escalate to critical */
  326. if (strstr (error_message, "Timeout"))
  327. result = STATE_CRITICAL;
  328. }
  329. else if (result == STATE_WARNING)
  330. printf ("%s (%s)\n", error_message, display_message);
  331. return result;
  332. }
  333. /* process command-line arguments */
  334. int
  335. process_arguments (int argc, char **argv)
  336. {
  337. int c;
  338. #ifdef HAVE_GETOPT_H
  339. int option_index = 0;
  340. static struct option long_options[] = {
  341. {"hostname", required_argument, 0, 'H'},
  342. {"expect", required_argument, 0, 'e'},
  343. /* {"critical", required_argument,0,'c'}, */
  344. /* {"warning", required_argument,0,'w'}, */
  345. /* {"port", required_argument,0,'P'}, */
  346. {"verbose", no_argument, 0, 'v'},
  347. {"version", no_argument, 0, 'V'},
  348. {"help", no_argument, 0, 'h'},
  349. {0, 0, 0, 0}
  350. };
  351. #endif
  352. if (argc < 2)
  353. return ERROR;
  354. for (c = 1; c < argc; c++) {
  355. if (strcmp ("-to", argv[c]) == 0)
  356. strcpy (argv[c], "-t");
  357. else if (strcmp ("-wt", argv[c]) == 0)
  358. strcpy (argv[c], "-w");
  359. else if (strcmp ("-ct", argv[c]) == 0)
  360. strcpy (argv[c], "-c");
  361. }
  362. while (1) {
  363. #ifdef HAVE_GETOPT_H
  364. c = getopt_long (argc, argv, "+hVH:C:", long_options, &option_index);
  365. #else
  366. c = getopt (argc, argv, "+?hVH:C:");
  367. #endif
  368. if (c == -1 || c == EOF || c == 1)
  369. break;
  370. switch (c) {
  371. case 'H': /* hostname */
  372. if (is_host (optarg)) {
  373. address = optarg;
  374. }
  375. else {
  376. usage ("Invalid host name\n");
  377. }
  378. break;
  379. case 'C': /* community */
  380. community = optarg;
  381. break;
  382. case 'V': /* version */
  383. print_revision (PROGNAME, REVISION);
  384. exit (STATE_OK);
  385. case 'h': /* help */
  386. print_help ();
  387. exit (STATE_OK);
  388. case '?': /* help */
  389. usage ("Invalid argument\n");
  390. }
  391. }
  392. c = optind;
  393. if (address == NULL) {
  394. if (is_host (argv[c])) {
  395. address = argv[c++];
  396. }
  397. else {
  398. usage ("Invalid host name");
  399. }
  400. }
  401. if (community == NULL) {
  402. community = argv[c++];
  403. }
  404. return validate_arguments ();
  405. }
  406. int
  407. validate_arguments (void)
  408. {
  409. return OK;
  410. }
  411. void
  412. print_help (void)
  413. {
  414. print_revision (PROGNAME, REVISION);
  415. printf
  416. ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
  417. "This plugin tests the STATUS of an HP printer with a JetDirect card.\n"
  418. "Net-snmp must be installed on the computer running the plugin.\n\n");
  419. print_usage ();
  420. printf
  421. ("\nOptions:\n"
  422. " -H, --hostname=STRING or IPADDRESS\n"
  423. " Check server on the indicated host\n"
  424. " -C, --community=STRING\n"
  425. " The SNMP community name\n"
  426. " -h, --help\n"
  427. " Print detailed help screen\n"
  428. " -V, --version\n" " Print version information\n\n");
  429. support ();
  430. }
  431. void
  432. print_usage (void)
  433. {
  434. printf
  435. ("Usage: %s -H host [-C community]\n"
  436. " %s --help\n"
  437. " %s --version\n", PROGNAME, PROGNAME, PROGNAME);
  438. }
  439. /*
  440. if(argc<2||argc>3){
  441. printf("Incorrect number of arguments supplied\n");
  442. printf("\n");
  443. print_revision(argv[0],"$Revision$");
  444. printf("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n");
  445. printf("License: GPL\n");
  446. printf("\n");
  447. printf("Usage: %s <ip_address> [community]\n",argv[0]);
  448. printf("\n");
  449. printf("Note:\n");
  450. printf(" <ip_address> = The IP address of the JetDirect card\n");
  451. printf(" [community] = An optional community string used for SNMP communication\n");
  452. printf(" with the JetDirect card. The default is 'public'.\n");
  453. printf("\n");
  454. return STATE_UNKNOWN;
  455. }
  456. // get the IP address of the JetDirect device
  457. strcpy(address,argv[1]);
  458. // get the community name to use for SNMP communication
  459. if(argc>=3)
  460. strcpy(community,argv[2]);
  461. else
  462. strcpy(community,"public");
  463. */