check_hpjd.c 9.9 KB

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