check_hpjd.c 13 KB

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