check_hpjd.c 10 KB

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