check_mrtgtraf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*****************************************************************************
  2. *
  3. * Nagios check_mrtgtraf 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_mtrgtraf plugin
  13. *
  14. * This plugin will check the incoming/outgoing transfer rates of a router
  15. * switch, etc recorded in an MRTG log.
  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. #include "common.h"
  35. #include "utils.h"
  36. const char *progname = "check_mrtgtraf";
  37. const char *revision = "$Revision$";
  38. const char *copyright = "1999-2007";
  39. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  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 = -1;
  46. int use_average = TRUE;
  47. unsigned long incoming_warning_threshold = 0L;
  48. unsigned long incoming_critical_threshold = 0L;
  49. unsigned long outgoing_warning_threshold = 0L;
  50. unsigned long outgoing_critical_threshold = 0L;
  51. int
  52. main (int argc, char **argv)
  53. {
  54. int result = STATE_OK;
  55. FILE *fp;
  56. int line;
  57. char input_buffer[MAX_INPUT_BUFFER];
  58. char *temp_buffer;
  59. time_t current_time;
  60. char *error_message;
  61. time_t timestamp = 0L;
  62. unsigned long average_incoming_rate = 0L;
  63. unsigned long average_outgoing_rate = 0L;
  64. unsigned long maximum_incoming_rate = 0L;
  65. unsigned long maximum_outgoing_rate = 0L;
  66. unsigned long incoming_rate = 0L;
  67. unsigned long outgoing_rate = 0L;
  68. double adjusted_incoming_rate = 0.0;
  69. double adjusted_outgoing_rate = 0.0;
  70. char incoming_speed_rating[8];
  71. char outgoing_speed_rating[8];
  72. setlocale (LC_ALL, "");
  73. bindtextdomain (PACKAGE, LOCALEDIR);
  74. textdomain (PACKAGE);
  75. if (process_arguments (argc, argv) == ERROR)
  76. usage4 (_("Could not parse arguments"));
  77. /* open the MRTG log file for reading */
  78. fp = fopen (log_file, "r");
  79. if (fp == NULL)
  80. usage4 (_("Unable to open MRTG log file"));
  81. line = 0;
  82. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  83. line++;
  84. /* skip the first line of the log file */
  85. if (line == 1)
  86. continue;
  87. /* break out of read loop */
  88. /* if we've passed the number of entries we want to read */
  89. if (line > 2)
  90. break;
  91. /* grab the timestamp */
  92. temp_buffer = strtok (input_buffer, " ");
  93. timestamp = strtoul (temp_buffer, NULL, 10);
  94. /* grab the average incoming transfer rate */
  95. temp_buffer = strtok (NULL, " ");
  96. average_incoming_rate = strtoul (temp_buffer, NULL, 10);
  97. /* grab the average outgoing transfer rate */
  98. temp_buffer = strtok (NULL, " ");
  99. average_outgoing_rate = strtoul (temp_buffer, NULL, 10);
  100. /* grab the maximum incoming transfer rate */
  101. temp_buffer = strtok (NULL, " ");
  102. maximum_incoming_rate = strtoul (temp_buffer, NULL, 10);
  103. /* grab the maximum outgoing transfer rate */
  104. temp_buffer = strtok (NULL, " ");
  105. maximum_outgoing_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. usage4 (_("Unable to process MRTG log file"));
  112. /* make sure the MRTG data isn't too old */
  113. time (&current_time);
  114. if ((expire_minutes > 0) &&
  115. (current_time - timestamp) > (expire_minutes * 60))
  116. die (STATE_WARNING, _("MRTG data has expired (%d minutes old)\n"),
  117. (int) ((current_time - timestamp) / 60));
  118. /* else check the incoming/outgoing rates */
  119. if (use_average == TRUE) {
  120. incoming_rate = average_incoming_rate;
  121. outgoing_rate = average_outgoing_rate;
  122. }
  123. else {
  124. incoming_rate = maximum_incoming_rate;
  125. outgoing_rate = maximum_outgoing_rate;
  126. }
  127. /* report incoming traffic in Bytes/sec */
  128. if (incoming_rate < 1024) {
  129. strcpy (incoming_speed_rating, "B/s");
  130. adjusted_incoming_rate = (double) incoming_rate;
  131. }
  132. /* report incoming traffic in KBytes/sec */
  133. else if (incoming_rate < (1024 * 1024)) {
  134. strcpy (incoming_speed_rating, "KB/s");
  135. adjusted_incoming_rate = (double) (incoming_rate / 1024.0);
  136. }
  137. /* report incoming traffic in MBytes/sec */
  138. else {
  139. strcpy (incoming_speed_rating, "MB/s");
  140. adjusted_incoming_rate = (double) (incoming_rate / 1024.0 / 1024.0);
  141. }
  142. /* report outgoing traffic in Bytes/sec */
  143. if (outgoing_rate < 1024) {
  144. strcpy (outgoing_speed_rating, "B/s");
  145. adjusted_outgoing_rate = (double) outgoing_rate;
  146. }
  147. /* report outgoing traffic in KBytes/sec */
  148. else if (outgoing_rate < (1024 * 1024)) {
  149. strcpy (outgoing_speed_rating, "KB/s");
  150. adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0);
  151. }
  152. /* report outgoing traffic in MBytes/sec */
  153. else {
  154. strcpy (outgoing_speed_rating, "MB/s");
  155. adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0 / 1024.0);
  156. }
  157. if (incoming_rate > incoming_critical_threshold
  158. || outgoing_rate > outgoing_critical_threshold) {
  159. result = STATE_CRITICAL;
  160. }
  161. else if (incoming_rate > incoming_warning_threshold
  162. || outgoing_rate > outgoing_warning_threshold) {
  163. result = STATE_WARNING;
  164. }
  165. asprintf (&error_message, _("%s. In = %0.1f %s, %s. Out = %0.1f %s|%s %s\n"),
  166. (use_average == TRUE) ? _("Avg") : _("Max"), adjusted_incoming_rate,
  167. incoming_speed_rating, (use_average == TRUE) ? _("Avg") : _("Max"),
  168. adjusted_outgoing_rate, outgoing_speed_rating,
  169. fperfdata("in", adjusted_incoming_rate, incoming_speed_rating,
  170. (int)incoming_warning_threshold, incoming_warning_threshold,
  171. (int)incoming_critical_threshold, incoming_critical_threshold,
  172. TRUE, 0, FALSE, 0),
  173. fperfdata("in", adjusted_outgoing_rate, outgoing_speed_rating,
  174. (int)outgoing_warning_threshold, outgoing_warning_threshold,
  175. (int)outgoing_critical_threshold, outgoing_critical_threshold,
  176. TRUE, 0, FALSE, 0));
  177. printf (_("Traffic %s - %s\n"), state_text(result), error_message);
  178. return result;
  179. }
  180. /* process command-line arguments */
  181. int
  182. process_arguments (int argc, char **argv)
  183. {
  184. int c;
  185. int option = 0;
  186. static struct option longopts[] = {
  187. {"logfile", required_argument, 0, 'F'},
  188. {"expires", required_argument, 0, 'e'},
  189. {"aggregation", required_argument, 0, 'a'},
  190. {"variable", required_argument, 0, 'v'},
  191. {"critical", required_argument, 0, 'c'},
  192. {"warning", required_argument, 0, 'w'},
  193. {"verbose", no_argument, 0, 'v'},
  194. {"version", no_argument, 0, 'V'},
  195. {"help", no_argument, 0, 'h'},
  196. {0, 0, 0, 0}
  197. };
  198. if (argc < 2)
  199. return ERROR;
  200. for (c = 1; c < argc; c++) {
  201. if (strcmp ("-to", argv[c]) == 0)
  202. strcpy (argv[c], "-t");
  203. else if (strcmp ("-wt", argv[c]) == 0)
  204. strcpy (argv[c], "-w");
  205. else if (strcmp ("-ct", argv[c]) == 0)
  206. strcpy (argv[c], "-c");
  207. }
  208. while (1) {
  209. c = getopt_long (argc, argv, "hVF:e:a:c:w:", longopts, &option);
  210. if (c == -1 || c == EOF)
  211. break;
  212. switch (c) {
  213. case 'F': /* input file */
  214. log_file = optarg;
  215. break;
  216. case 'e': /* expiration time */
  217. expire_minutes = atoi (optarg);
  218. break;
  219. case 'a': /* aggregation (AVE or MAX) */
  220. if (!strcmp (optarg, "MAX"))
  221. use_average = FALSE;
  222. else
  223. use_average = TRUE;
  224. break;
  225. case 'c': /* warning threshold */
  226. sscanf (optarg, "%lu,%lu", &incoming_critical_threshold,
  227. &outgoing_critical_threshold);
  228. break;
  229. case 'w': /* critical threshold */
  230. sscanf (optarg, "%lu,%lu", &incoming_warning_threshold,
  231. &outgoing_warning_threshold);
  232. break;
  233. case 'V': /* version */
  234. print_revision (progname, revision);
  235. exit (STATE_OK);
  236. case 'h': /* help */
  237. print_help ();
  238. exit (STATE_OK);
  239. case '?': /* help */
  240. usage5 ();
  241. }
  242. }
  243. c = optind;
  244. if (argc > c && log_file == NULL) {
  245. log_file = argv[c++];
  246. }
  247. if (argc > c && expire_minutes == -1) {
  248. expire_minutes = atoi (argv[c++]);
  249. }
  250. if (argc > c && strcmp (argv[c], "MAX") == 0) {
  251. use_average = FALSE;
  252. c++;
  253. }
  254. else if (argc > c && strcmp (argv[c], "AVG") == 0) {
  255. use_average = TRUE;
  256. c++;
  257. }
  258. if (argc > c && incoming_warning_threshold == 0) {
  259. incoming_warning_threshold = strtoul (argv[c++], NULL, 10);
  260. }
  261. if (argc > c && incoming_critical_threshold == 0) {
  262. incoming_critical_threshold = strtoul (argv[c++], NULL, 10);
  263. }
  264. if (argc > c && outgoing_warning_threshold == 0) {
  265. outgoing_warning_threshold = strtoul (argv[c++], NULL, 10);
  266. }
  267. if (argc > c && outgoing_critical_threshold == 0) {
  268. outgoing_critical_threshold = strtoul (argv[c++], NULL, 10);
  269. }
  270. return validate_arguments ();
  271. }
  272. int
  273. validate_arguments (void)
  274. {
  275. return OK;
  276. }
  277. void
  278. print_help (void)
  279. {
  280. print_revision (progname, revision);
  281. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  282. printf (COPYRIGHT, copyright, email);
  283. printf ("%s\n", _("This plugin will check the incoming/outgoing transfer rates of a router,"));
  284. printf ("%s\n", _("switch, etc recorded in an MRTG log. If the newest log entry is older"));
  285. printf ("%s\n", _("than <expire_minutes>, a WARNING status is returned. If either the"));
  286. printf ("%s\n", _("incoming or outgoing rates exceed the <icl> or <ocl> thresholds (in"));
  287. printf ("%s\n", _("Bytes/sec), a CRITICAL status results. If either of the rates exceed"));
  288. printf ("%s\n", _("the <iwl> or <owl> thresholds (in Bytes/sec), a WARNING status results."));
  289. printf ("\n\n");
  290. print_usage ();
  291. printf (_(UT_HELP_VRSN));
  292. printf (" %s\n", "-F, --filename=STRING");
  293. printf (" %s\n", _("File to read log from"));
  294. printf (" %s\n", "-e, --expires=INTEGER");
  295. printf (" %s\n", _("Minutes after which log expires"));
  296. printf (" %s\n", "-a, --aggregation=(AVG|MAX)");
  297. printf (" %s\n", _("Test average or maximum"));
  298. printf (" %s\n", "-w, --warning");
  299. printf (" %s\n", _("Warning threshold pair <incoming>,<outgoing>"));
  300. printf (" %s\n", "-c, --critical");
  301. printf (" %s\n", _("Critical threshold pair <incoming>,<outgoing>"));
  302. printf ("%s\n", _("Notes:"));
  303. printf (" %s\n", _("- MRTG stands for Multi Router Traffic Grapher. It can be downloaded from"));
  304. printf (" %s\n", " http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html");
  305. printf (" %s\n", _("- While MRTG can monitor things other than traffic rates, this"));
  306. printf (" %s\n", _(" plugin probably won't work with much else without modification."));
  307. printf (" %s\n", _("- The calculated i/o rates are a little off from what MRTG actually"));
  308. printf (" %s\n", _(" reports. I'm not sure why this is right now, but will look into it"));
  309. printf (" %s\n", _(" for future enhancements of this plugin."));
  310. printf (_(UT_SUPPORT));
  311. }
  312. void
  313. print_usage (void)
  314. {
  315. printf (_("Usage"));
  316. printf (" %s -F <log_file> -a <AVG | MAX> -v <variable> -w <warning_pair>",progname);
  317. printf ("-c <critical_pair> [-e expire_minutes] [-t timeout] [-v]\n");
  318. }