4
0

check_uptime.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*********************************************************************************
  2. *
  3. * Nagios check_uptime plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2013-2014 Nagios Plugin Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains the check_uptime plugin
  11. *
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. *********************************************************************************/
  27. #include "common.h"
  28. #include "utils.h"
  29. #include "utils_base.h"
  30. #include <time.h>
  31. char *progname = "check_uptime";
  32. char *version = "1.0";
  33. char *email = "devel@nagios-plugins.org";
  34. char *copyright = "2014";
  35. /* There no developer in COPYRIGHT's definition
  36. * char *developer = "Andy Brist";
  37. */
  38. static int process_arguments (int, char **);
  39. int validate_arguments (void);
  40. int getuptime (void);
  41. void print_help (void);
  42. void print_usage (void);
  43. int verbose = 0;
  44. char *warning, *critical, *timeunit;
  45. thresholds *my_thresholds = NULL;
  46. int main (int argc, char **argv) {
  47. // base values
  48. int status, intvalue;
  49. int upminutes, uphours, updays;
  50. double value, uptime;
  51. char* perf;
  52. char* output_message;
  53. /* Parse extra opts if any */
  54. argv = np_extra_opts (&argc, argv, progname);
  55. if (process_arguments (argc, argv) == ERROR)
  56. usage4 (_("Could not parse arguments"));
  57. /* Set signal handling and alarm timeout */
  58. if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
  59. die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));
  60. }
  61. alarm (timeout_interval);
  62. value = getuptime();
  63. if (verbose >= 3) {
  64. printf("Uptime in seconds returned from timespec struct: %f\n", value);
  65. }
  66. intvalue = (int)value;
  67. updays = intvalue / 86400;
  68. uphours = (intvalue % 86400) / 3600;
  69. upminutes = ((intvalue % 86400) % 3600) / 60;
  70. if (!strncmp(timeunit, "minutes", strlen("minutes"))) {
  71. uptime = intvalue / 60;
  72. } else if (!strncmp(timeunit, "hours", strlen("hours"))) {
  73. uptime = intvalue / 3600;
  74. } else if (!strncmp(timeunit, "days", strlen("days"))) {
  75. uptime = intvalue / 86400;
  76. } else {
  77. uptime = intvalue;
  78. }
  79. xasprintf(&output_message,_("%u day(s) %u hour(s) %u minute(s)"), updays, uphours, upminutes);
  80. xasprintf(&perf,_("%s"),
  81. fperfdata("uptime", uptime, "",
  82. my_thresholds->warning?TRUE:FALSE, my_thresholds->warning?my_thresholds->warning->end:0,
  83. my_thresholds->critical?TRUE:FALSE, my_thresholds->critical?my_thresholds->critical->end:0,
  84. FALSE, 0,
  85. FALSE, 0)
  86. );
  87. status = get_status(uptime, my_thresholds);
  88. if (status == STATE_OK) {
  89. printf("Uptime %s: %s | %s\n", _("OK"), output_message, perf);
  90. } else if (status == STATE_WARNING) {
  91. printf("Uptime %s: %s | %s\n", _("WARNING"), output_message, perf);
  92. } else if (status == STATE_CRITICAL) {
  93. printf("Uptime %s: %s | %s\n", _("CRITICAL"), output_message, perf);
  94. }
  95. return status;
  96. } // end main
  97. int getuptime () {
  98. struct timespec t;
  99. clock_gettime(CLOCK_MONOTONIC, &t);
  100. if (t.tv_sec > 0) {
  101. return t.tv_sec;
  102. } else {
  103. printf("Uptime UNKNOWN: Timespec struct failed to retrieve uptime\n");
  104. exit (STATE_UNKNOWN);
  105. }
  106. } // end getuptime
  107. static int process_arguments (int argc, char **argv) {
  108. int c;
  109. int option = 0;
  110. static struct option longopts[] = {
  111. {"critical", required_argument, 0, 'c'},
  112. {"warning", required_argument, 0, 'w'},
  113. {"timeout", required_argument, 0, 't'},
  114. {"timeunit", required_argument, 0, 'u'},
  115. {"verbose", no_argument, 0, 'v'},
  116. {"version", no_argument, 0, 'V'},
  117. {"help", no_argument, 0, 'h'},
  118. {0, 0, 0, 0}
  119. };
  120. while ( 1 ) {
  121. c = getopt_long ( argc, argv, "+hvVu:c:w:t:", longopts, &option );
  122. if ( c == -1 || c == EOF || c == 1 ) break;
  123. switch ( c ) {
  124. case '?':
  125. usage5 ();
  126. case 'h':
  127. print_help ();
  128. exit ( STATE_OK );
  129. case 'v':
  130. verbose++;
  131. if (verbose >= 3) {
  132. printf("Verbose mode enabled\n");
  133. }
  134. break;
  135. case 'V':
  136. print_revision (progname, NP_VERSION);
  137. exit (STATE_OK);
  138. case 'u':
  139. timeunit = optarg;
  140. break;
  141. case 'c':
  142. critical = optarg;
  143. break;
  144. case 'w':
  145. warning = optarg;
  146. break;
  147. case 't': /* timeout period */
  148. timeout_interval = parse_timeout_string (optarg);
  149. break;
  150. } // end case
  151. } // end while
  152. set_thresholds(&my_thresholds, warning, critical);
  153. return validate_arguments ();
  154. } // end process_arguments
  155. int validate_arguments (void) {
  156. if (timeunit == NULL) {
  157. timeunit = "minutes";
  158. if (verbose >= 3) {
  159. printf("No unit of time measurement specified. Using default: \"minutes\"\n");
  160. }
  161. } else if (strncmp(timeunit, "seconds", strlen("seconds") + 1 ) &&
  162. strncmp(timeunit, "minutes", strlen("minutes") + 1) &&
  163. strncmp(timeunit, "hours", strlen("hours") + 1) &&
  164. strncmp(timeunit, "days", strlen("days") + 1)) {
  165. if (verbose >= 3) {
  166. printf("Invalid unit of time measurement specified: \"%s\"\n", timeunit);
  167. }
  168. usage4(_("Wrong -u argument, expected: seconds, minutes, hours, or days"));
  169. } else if (verbose >= 3) {
  170. printf("Specified unit of time measurement accepted: \"%s\"\n", timeunit);
  171. }
  172. return OK;
  173. } //end validate
  174. void print_usage (void) {
  175. printf( "%s\n", _("Usage:") );
  176. printf( "%s", _("check_uptime ") );
  177. printf( "%s\n", _("[-u uom] [-w threshold] [-c threshold] [-t] [-h] [-vvv] [-V]") );
  178. } // end usage
  179. void print_help (void) {
  180. print_revision ( progname, NP_VERSION );
  181. printf ( COPYRIGHT, copyright, email );
  182. printf ( "%s\n", _("This plugin checks the system uptime and alerts if more than the threshold.") );
  183. printf ( "%s\n", _("Threshold unit of measurement specified with \"-u\".") );
  184. printf ( "%s\n", _("\"-u\" switch supports: seconds|minutes|hours|days.") );
  185. print_usage ();
  186. printf ( UT_HELP_VRSN );
  187. printf ( UT_EXTRA_OPTS );
  188. printf ( "%s\n", _("-u, Time unit of measurement (seconds|minutes|hours|days) (default: minutes)") );
  189. printf ( "%s\n", _("-w, Warning threshold") );
  190. printf ( "%s\n", _("-c, Critcal threshold") );
  191. printf ( "%s\n", _("-t, Plugin timeout, default 10 seconds") );
  192. printf ( "%s\n", _("-vvv, Enable verbose output") );
  193. //printf ( "%s\n", _("-h, Print help and usage") );
  194. printf ( UT_SUPPORT );
  195. } // end print_help