check_hpjd.c 12 KB

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