check_mrtg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*****************************************************************************
  2. *
  3. * Nagios check_mrtg plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2007 Nagios Plugins Development Team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_mrtg plugin
  13. *
  14. * This plugin will check either the average or maximum value of one of the
  15. * two variables recorded in an MRTG log file.
  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_mrtg";
  35. const char *revision = "$Revision$";
  36. const char *copyright = "1999-2007";
  37. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  38. #include "common.h"
  39. #include "utils.h"
  40. int process_arguments (int, char **);
  41. int validate_arguments (void);
  42. void print_help (void);
  43. void print_usage (void);
  44. char *log_file = NULL;
  45. int expire_minutes = 0;
  46. int use_average = TRUE;
  47. int variable_number = -1;
  48. unsigned long value_warning_threshold = 0L;
  49. unsigned long value_critical_threshold = 0L;
  50. char *label;
  51. char *units;
  52. int
  53. main (int argc, char **argv)
  54. {
  55. int result = STATE_UNKNOWN;
  56. FILE *fp;
  57. int line;
  58. char input_buffer[MAX_INPUT_BUFFER];
  59. char *temp_buffer;
  60. time_t current_time;
  61. time_t timestamp = 0L;
  62. unsigned long average_value_rate = 0L;
  63. unsigned long maximum_value_rate = 0L;
  64. unsigned long rate = 0L;
  65. setlocale (LC_ALL, "");
  66. bindtextdomain (PACKAGE, LOCALEDIR);
  67. textdomain (PACKAGE);
  68. /* Parse extra opts if any */
  69. argv=np_extra_opts (&argc, argv, progname);
  70. if (process_arguments (argc, argv) == ERROR)
  71. usage4 (_("Could not parse arguments\n"));
  72. /* open the MRTG log file for reading */
  73. fp = fopen (log_file, "r");
  74. if (fp == NULL) {
  75. printf (_("Unable to open MRTG log file\n"));
  76. return STATE_UNKNOWN;
  77. }
  78. line = 0;
  79. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  80. line++;
  81. /* skip the first line of the log file */
  82. if (line == 1)
  83. continue;
  84. /* break out of read loop if we've passed the number of entries we want to read */
  85. if (line > 2)
  86. break;
  87. /* grab the timestamp */
  88. temp_buffer = strtok (input_buffer, " ");
  89. timestamp = strtoul (temp_buffer, NULL, 10);
  90. /* grab the average value 1 rate */
  91. temp_buffer = strtok (NULL, " ");
  92. if (variable_number == 1)
  93. average_value_rate = strtoul (temp_buffer, NULL, 10);
  94. /* grab the average value 2 rate */
  95. temp_buffer = strtok (NULL, " ");
  96. if (variable_number == 2)
  97. average_value_rate = strtoul (temp_buffer, NULL, 10);
  98. /* grab the maximum value 1 rate */
  99. temp_buffer = strtok (NULL, " ");
  100. if (variable_number == 1)
  101. maximum_value_rate = strtoul (temp_buffer, NULL, 10);
  102. /* grab the maximum value 2 rate */
  103. temp_buffer = strtok (NULL, " ");
  104. if (variable_number == 2)
  105. maximum_value_rate = strtoul (temp_buffer, NULL, 10);
  106. }
  107. /* close the log file */
  108. fclose (fp);
  109. /* if we couldn't read enough data, return an unknown error */
  110. if (line <= 2) {
  111. printf (_("Unable to process MRTG log file\n"));
  112. return STATE_UNKNOWN;
  113. }
  114. /* make sure the MRTG data isn't too old */
  115. time (&current_time);
  116. if (expire_minutes > 0
  117. && (current_time - timestamp) > (expire_minutes * 60)) {
  118. printf (_("MRTG data has expired (%d minutes old)\n"),
  119. (int) ((current_time - timestamp) / 60));
  120. return STATE_WARNING;
  121. }
  122. /* else check the incoming/outgoing rates */
  123. if (use_average == TRUE)
  124. rate = average_value_rate;
  125. else
  126. rate = maximum_value_rate;
  127. if (rate > value_critical_threshold)
  128. result = STATE_CRITICAL;
  129. else if (rate > value_warning_threshold)
  130. result = STATE_WARNING;
  131. printf("%s. %s = %lu %s|%s\n",
  132. (use_average == TRUE) ? _("Avg") : _("Max"),
  133. label, rate, units,
  134. perfdata(label, (long) rate, units,
  135. (int) value_warning_threshold, (long) value_warning_threshold,
  136. (int) value_critical_threshold, (long) value_critical_threshold,
  137. 0, 0, 0, 0));
  138. return result;
  139. }
  140. /* process command-line arguments */
  141. int
  142. process_arguments (int argc, char **argv)
  143. {
  144. int c;
  145. int option = 0;
  146. static struct option longopts[] = {
  147. {"logfile", required_argument, 0, 'F'},
  148. {"expires", required_argument, 0, 'e'},
  149. {"aggregation", required_argument, 0, 'a'},
  150. {"variable", required_argument, 0, 'v'},
  151. {"critical", required_argument, 0, 'c'},
  152. {"warning", required_argument, 0, 'w'},
  153. {"label", required_argument, 0, 'l'},
  154. {"units", required_argument, 0, 'u'},
  155. {"verbose", no_argument, 0, 'v'},
  156. {"version", no_argument, 0, 'V'},
  157. {"help", no_argument, 0, 'h'},
  158. {0, 0, 0, 0}
  159. };
  160. if (argc < 2)
  161. return ERROR;
  162. for (c = 1; c < argc; c++) {
  163. if (strcmp ("-to", argv[c]) == 0)
  164. strcpy (argv[c], "-t");
  165. else if (strcmp ("-wt", argv[c]) == 0)
  166. strcpy (argv[c], "-w");
  167. else if (strcmp ("-ct", argv[c]) == 0)
  168. strcpy (argv[c], "-c");
  169. }
  170. while (1) {
  171. c = getopt_long (argc, argv, "hVF:e:a:v:c:w:l:u:", longopts,
  172. &option);
  173. if (c == -1 || c == EOF)
  174. break;
  175. switch (c) {
  176. case 'F': /* input file */
  177. log_file = optarg;
  178. break;
  179. case 'e': /* ups name */
  180. expire_minutes = atoi (optarg);
  181. break;
  182. case 'a': /* port */
  183. if (!strcmp (optarg, "MAX"))
  184. use_average = FALSE;
  185. else
  186. use_average = TRUE;
  187. break;
  188. case 'v':
  189. variable_number = atoi (optarg);
  190. if (variable_number < 1 || variable_number > 2)
  191. usage4 (_("Invalid variable number"));
  192. break;
  193. case 'w': /* critical time threshold */
  194. value_warning_threshold = strtoul (optarg, NULL, 10);
  195. break;
  196. case 'c': /* warning time threshold */
  197. value_critical_threshold = strtoul (optarg, NULL, 10);
  198. break;
  199. case 'l': /* label */
  200. label = optarg;
  201. break;
  202. case 'u': /* timeout */
  203. units = optarg;
  204. break;
  205. case 'V': /* version */
  206. print_revision (progname, revision);
  207. exit (STATE_OK);
  208. case 'h': /* help */
  209. print_help ();
  210. exit (STATE_OK);
  211. case '?': /* help */
  212. usage5 ();
  213. }
  214. }
  215. c = optind;
  216. if (log_file == NULL && argc > c) {
  217. log_file = argv[c++];
  218. }
  219. if (expire_minutes <= 0 && argc > c) {
  220. if (is_intpos (argv[c]))
  221. expire_minutes = atoi (argv[c++]);
  222. else
  223. die (STATE_UNKNOWN,
  224. _("%s is not a valid expiration time\nUse '%s -h' for additional help\n"),
  225. argv[c], progname);
  226. }
  227. if (argc > c && strcmp (argv[c], "MAX") == 0) {
  228. use_average = FALSE;
  229. c++;
  230. }
  231. else if (argc > c && strcmp (argv[c], "AVG") == 0) {
  232. use_average = TRUE;
  233. c++;
  234. }
  235. if (argc > c && variable_number == -1) {
  236. variable_number = atoi (argv[c++]);
  237. if (variable_number < 1 || variable_number > 2) {
  238. printf ("%s :", argv[c]);
  239. usage (_("Invalid variable number\n"));
  240. }
  241. }
  242. if (argc > c && value_warning_threshold == 0) {
  243. value_warning_threshold = strtoul (argv[c++], NULL, 10);
  244. }
  245. if (argc > c && value_critical_threshold == 0) {
  246. value_critical_threshold = strtoul (argv[c++], NULL, 10);
  247. }
  248. if (argc > c && strlen (label) == 0) {
  249. label = argv[c++];
  250. }
  251. if (argc > c && strlen (units) == 0) {
  252. units = argv[c++];
  253. }
  254. return validate_arguments ();
  255. }
  256. int
  257. validate_arguments (void)
  258. {
  259. if (variable_number == -1)
  260. usage4 (_("You must supply the variable number"));
  261. if (label == NULL)
  262. label = strdup ("value");
  263. if (units == NULL)
  264. units = strdup ("");
  265. return OK;
  266. }
  267. void
  268. print_help (void)
  269. {
  270. print_revision (progname, revision);
  271. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  272. printf (COPYRIGHT, copyright, email);
  273. printf ("%s\n", _("This plugin will check either the average or maximum value of one of the"));
  274. printf ("%s\n", _("two variables recorded in an MRTG log file."));
  275. printf ("\n\n");
  276. print_usage ();
  277. printf (_(UT_HELP_VRSN));
  278. printf (_(UT_EXTRA_OPTS));
  279. printf (" %s\n", "-F, --logfile=FILE");
  280. printf (" %s\n", _("The MRTG log file containing the data you want to monitor"));
  281. printf ("-e, --expires=MINUTES");
  282. printf (" %s\n", _("Minutes before MRTG data is considered to be too old"));
  283. printf (" %s\n", "-a, --aggregation=AVG|MAX");
  284. printf (" %s\n", _("Should we check average or maximum values?"));
  285. printf (" %s\n", "-v, --variable=INTEGER");
  286. printf (" %s\n", _("Which variable set should we inspect? (1 or 2)"));
  287. printf (" %s\n", "-w, --warning=INTEGER");
  288. printf (" %s\n", _("Threshold value for data to result in WARNING status"));
  289. printf (" %s\n", "-c, --critical=INTEGER");
  290. printf (" %s\n", _("Threshold value for data to result in CRITICAL status"));
  291. printf (" %s\n", "-l, --label=STRING");
  292. printf (" %s\n", _("Type label for data (Examples: Conns, \"Processor Load\", In, Out)"));
  293. printf (" %s\n", "-u, --units=STRING");
  294. printf (" %s\n", _("Option units label for data (Example: Packets/Sec, Errors/Sec,"));
  295. printf (" %s\n", _("\"Bytes Per Second\", \"%% Utilization\")"));
  296. printf ("\n");
  297. printf (" %s\n", _("If the value exceeds the <vwl> threshold, a WARNING status is returned. If"));
  298. printf (" %s\n", _("the value exceeds the <vcl> threshold, a CRITICAL status is returned. If"));
  299. printf (" %s\n", _("the data in the log file is older than <expire_minutes> old, a WARNING"));
  300. printf (" %s\n", _("status is returned and a warning message is printed."));
  301. printf ("\n");
  302. printf (" %s\n", _("This plugin is useful for monitoring MRTG data that does not correspond to"));
  303. printf (" %s\n", _("bandwidth usage. (Use the check_mrtgtraf plugin for monitoring bandwidth)."));
  304. printf (" %s\n", _("It can be used to monitor any kind of data that MRTG is monitoring - errors,"));
  305. printf (" %s\n", _("packets/sec, etc. I use MRTG in conjuction with the Novell NLM that allows"));
  306. printf (" %s\n", _("me to track processor utilization, user connections, drive space, etc and"));
  307. printf (" %s\n\n", _("this plugin works well for monitoring that kind of data as well."));
  308. printf ("%s\n", _("Notes:"));
  309. printf (" %s\n", _("- This plugin only monitors one of the two variables stored in the MRTG log"));
  310. printf (" %s\n", _("file. If you want to monitor both values you will have to define two"));
  311. printf (" %s\n", _("commands with different values for the <variable> argument. Of course,"));
  312. printf (" %s\n", _("you can always hack the code to make this plugin work for you..."));
  313. printf (" %s\n", _("- MRTG stands for the Multi Router Traffic Grapher. It can be downloaded from"));
  314. printf (" %s\n", "http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html");
  315. #ifdef NP_EXTRA_OPTS
  316. printf (" -%s", _(UT_EXTRA_OPTS_NOTES));
  317. #endif
  318. printf (_(UT_SUPPORT));
  319. }
  320. /* original command line:
  321. <log_file> <expire_minutes> <AVG|MAX> <variable> <vwl> <vcl> <label> [units] */
  322. void
  323. print_usage (void)
  324. {
  325. printf (_("Usage:"));
  326. printf ("%s -F log_file -a <AVG | MAX> -v variable -w warning -c critical\n",progname);
  327. printf ("[-l label] [-u units] [-e expire_minutes] [-t timeout] [-v]\n");
  328. }