check_hpjd.c 10 KB

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