check_hpjd.c 11 KB

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