check_hpjd.c 11 KB

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