check_hpjd.c 11 KB

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