check_hpjd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*****************************************************************************
  2. *
  3. * Nagios check_hpjd plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2014 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-2014";
  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. int port = 0;
  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. /* We want the string after the "=" */
  128. (void)strtok (input_buffer, "=");
  129. temp_buffer = strtok (NULL, "=");
  130. if (temp_buffer == NULL && line < 13) {
  131. result = STATE_UNKNOWN;
  132. strcpy (errmsg, input_buffer);
  133. } else {
  134. switch (line) {
  135. case 1: /* 1st line should contain the line status */
  136. line_status = atoi (temp_buffer);
  137. break;
  138. case 2: /* 2nd line should contain the paper status */
  139. paper_status = atoi (temp_buffer);
  140. break;
  141. case 3: /* 3rd line should be intervention required */
  142. intervention_required = atoi (temp_buffer);
  143. break;
  144. case 4: /* 4th line should be peripheral error */
  145. peripheral_error = atoi (temp_buffer);
  146. break;
  147. case 5: /* 5th line should contain the paper jam status */
  148. paper_jam = atoi (temp_buffer);
  149. break;
  150. case 6: /* 6th line should contain the paper out status */
  151. paper_out = atoi (temp_buffer);
  152. break;
  153. case 7: /* 7th line should contain the toner low status */
  154. toner_low = atoi (temp_buffer);
  155. break;
  156. case 8: /* did data come too slow for engine */
  157. page_punt = atoi (temp_buffer);
  158. break;
  159. case 9: /* did we run out of memory */
  160. memory_out = atoi (temp_buffer);
  161. break;
  162. case 10: /* is there a door open */
  163. door_open = atoi (temp_buffer);
  164. break;
  165. case 11: /* is output tray full */
  166. paper_output = atoi (temp_buffer);
  167. break;
  168. case 12: /* display panel message */
  169. strcpy (display_message, temp_buffer + 1);
  170. break;
  171. default: /* fold multiline message */
  172. strncat (display_message, input_buffer,
  173. sizeof (display_message) - strlen (display_message) - 1);
  174. }
  175. }
  176. /* break out of the read loop if we encounter an error */
  177. if (result != STATE_OK)
  178. break;
  179. }
  180. /* WARNING if output found on stderr */
  181. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  182. result = max_state (result, STATE_WARNING);
  183. /* remove CRLF */
  184. if (input_buffer[strlen (input_buffer) - 1] == '\n')
  185. input_buffer[strlen (input_buffer) - 1] = 0;
  186. sprintf (errmsg, "%s", input_buffer );
  187. }
  188. /* close stderr */
  189. (void) fclose (child_stderr);
  190. /* close the pipe */
  191. if (spclose (child_process))
  192. result = max_state (result, STATE_WARNING);
  193. /* if there wasn't any output, display an error */
  194. if (line == 0) {
  195. /* might not be the problem, but most likely is. */
  196. result = STATE_UNKNOWN ;
  197. xasprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address );
  198. }
  199. /* if we had no read errors, check the printer status results... */
  200. if (result == STATE_OK) {
  201. if (paper_jam) {
  202. result = STATE_WARNING;
  203. strcpy (errmsg, _("Paper Jam"));
  204. }
  205. else if (paper_out) {
  206. result = STATE_WARNING;
  207. strcpy (errmsg, _("Out of Paper"));
  208. }
  209. else if (line_status == OFFLINE) {
  210. if (strcmp (errmsg, "POWERSAVE ON") != 0) {
  211. result = STATE_WARNING;
  212. strcpy (errmsg, _("Printer Offline"));
  213. }
  214. }
  215. else if (peripheral_error) {
  216. result = STATE_WARNING;
  217. strcpy (errmsg, _("Peripheral Error"));
  218. }
  219. else if (intervention_required) {
  220. result = STATE_WARNING;
  221. strcpy (errmsg, _("Intervention Required"));
  222. }
  223. else if (toner_low) {
  224. result = STATE_WARNING;
  225. strcpy (errmsg, _("Toner Low"));
  226. }
  227. else if (memory_out) {
  228. result = STATE_WARNING;
  229. strcpy (errmsg, _("Insufficient Memory"));
  230. }
  231. else if (door_open) {
  232. result = STATE_WARNING;
  233. strcpy (errmsg, _("A Door is Open"));
  234. }
  235. else if (paper_output) {
  236. result = STATE_WARNING;
  237. strcpy (errmsg, _("Output Tray is Full"));
  238. }
  239. else if (page_punt) {
  240. result = STATE_WARNING;
  241. strcpy (errmsg, _("Data too Slow for Engine"));
  242. }
  243. else if (paper_status) {
  244. result = STATE_WARNING;
  245. strcpy (errmsg, _("Unknown Paper Error"));
  246. }
  247. }
  248. if (result == STATE_OK)
  249. printf (_("Printer ok - (%s)\n"), display_message);
  250. else if (result == STATE_UNKNOWN) {
  251. printf ("%s\n", errmsg);
  252. /* if printer could not be reached, escalate to critical */
  253. if (strstr (errmsg, "Timeout"))
  254. result = STATE_CRITICAL;
  255. }
  256. else if (result == STATE_WARNING)
  257. printf ("%s (%s)\n", errmsg, display_message);
  258. return result;
  259. }
  260. /* process command-line arguments */
  261. int
  262. process_arguments (int argc, char **argv)
  263. {
  264. int c;
  265. int option = 0;
  266. static struct option longopts[] = {
  267. {"hostname", required_argument, 0, 'H'},
  268. {"community", required_argument, 0, 'C'},
  269. /* {"critical", required_argument,0,'c'}, */
  270. /* {"warning", required_argument,0,'w'}, */
  271. {"port", required_argument,0,'p'},
  272. {"version", no_argument, 0, 'V'},
  273. {"help", no_argument, 0, 'h'},
  274. {0, 0, 0, 0}
  275. };
  276. if (argc < 2)
  277. return ERROR;
  278. while (1) {
  279. c = getopt_long (argc, argv, "+hVH:C:p:", longopts, &option);
  280. if (c == -1 || c == EOF || c == 1)
  281. break;
  282. switch (c) {
  283. case 'H': /* hostname */
  284. if (is_host (optarg)) {
  285. address = strscpy(address, optarg) ;
  286. }
  287. else {
  288. usage2 (_("Invalid hostname/address"), optarg);
  289. }
  290. break;
  291. case 'C': /* community */
  292. community = strscpy (community, optarg);
  293. break;
  294. case 'p':
  295. if (!is_intpos(optarg))
  296. usage2 (_("Port must be a positive short integer"), optarg);
  297. else
  298. port = atoi(optarg);
  299. break;
  300. case 'V': /* version */
  301. print_revision (progname, NP_VERSION);
  302. exit (STATE_OK);
  303. case 'h': /* help */
  304. print_help ();
  305. exit (STATE_OK);
  306. case '?': /* help */
  307. usage5 ();
  308. }
  309. }
  310. c = optind;
  311. if (address == NULL) {
  312. if (is_host (argv[c])) {
  313. address = argv[c++];
  314. }
  315. else {
  316. usage2 (_("Invalid hostname/address"), argv[c]);
  317. }
  318. }
  319. if (community == NULL) {
  320. if (argv[c] != NULL )
  321. community = argv[c++];
  322. else
  323. community = strdup (DEFAULT_COMMUNITY);
  324. }
  325. if (port == 0) {
  326. if (argv[c] != NULL )
  327. port = atoi (argv[c++]);
  328. else
  329. port = atoi (DEFAULT_PORT);
  330. }
  331. return validate_arguments ();
  332. }
  333. int
  334. validate_arguments (void)
  335. {
  336. return OK;
  337. }
  338. void
  339. print_help (void)
  340. {
  341. print_revision (progname, NP_VERSION);
  342. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  343. printf (COPYRIGHT, copyright, email);
  344. printf ("%s\n", _("This plugin tests the STATUS of an HP printer with a JetDirect card."));
  345. printf ("%s\n", _("Net-snmp must be installed on the computer running the plugin."));
  346. printf ("\n\n");
  347. print_usage ();
  348. printf (UT_HELP_VRSN);
  349. printf (UT_EXTRA_OPTS);
  350. printf (" %s\n", "-C, --community=STRING");
  351. printf (" %s", _("The SNMP community name "));
  352. printf (_("(default=%s)"), DEFAULT_COMMUNITY);
  353. printf ("\n");
  354. printf (" %s\n", "-p, --port=STRING");
  355. printf (" %s", _("Specify the port to check "));
  356. printf (_("(default=%s)"), DEFAULT_PORT);
  357. printf ("\n");
  358. printf (UT_SUPPORT);
  359. }
  360. void
  361. print_usage (void)
  362. {
  363. printf ("%s\n", _("Usage:"));
  364. printf ("%s -H host [-C community] [-p port]\n", progname);
  365. }