check_mrtg.c 11 KB

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