check_hpjd.c 11 KB

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