Back to index

nagios-plugins  1.4.16
check_users.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * Nagios check_users plugin
00004 * 
00005 * License: GPL
00006 * Copyright (c) 2000-2012 Nagios Plugins Development Team
00007 * 
00008 * Description:
00009 * 
00010 * This file contains the check_users plugin
00011 * 
00012 * This plugin checks the number of users currently logged in on the local
00013 * system and generates an error if the number exceeds the thresholds
00014 * specified.
00015 * 
00016 * 
00017 * This program is free software: you can redistribute it and/or modify
00018 * it under the terms of the GNU General Public License as published by
00019 * the Free Software Foundation, either version 3 of the License, or
00020 * (at your option) any later version.
00021 * 
00022 * This program is distributed in the hope that it will be useful,
00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 * GNU General Public License for more details.
00026 * 
00027 * You should have received a copy of the GNU General Public License
00028 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00029 * 
00030 * 
00031 *****************************************************************************/
00032 
00033 const char *progname = "check_users";
00034 const char *copyright = "2000-2007";
00035 const char *email = "nagiosplug-devel@lists.sourceforge.net";
00036 
00037 #include "common.h"
00038 #include "utils.h"
00039 #include <utmpx.h>
00040 
00041 #define possibly_set(a,b) ((a) == 0 ? (b) : 0)
00042 
00043 int process_arguments (int, char **);
00044 void print_help (void);
00045 void print_usage (void);
00046 
00047 int wusers = -1;
00048 int cusers = -1;
00049 
00050 int
00051 main (int argc, char **argv)
00052 {
00053        int users = -1;
00054        int result = STATE_UNKNOWN;
00055        char *perf;
00056        struct utmpx *putmpx;
00057 
00058        setlocale (LC_ALL, "");
00059        bindtextdomain (PACKAGE, LOCALEDIR);
00060        textdomain (PACKAGE);
00061 
00062        perf = strdup ("");
00063 
00064        /* Parse extra opts if any */
00065        argv = np_extra_opts (&argc, argv, progname);
00066 
00067        if (process_arguments (argc, argv) == ERROR)
00068               usage4 (_("Could not parse arguments"));
00069 
00070        users = 0;
00071 
00072        /* get currently logged users from utmpx */
00073        setutxent ();
00074 
00075        while ((putmpx = getutxent ()) != NULL)
00076               if (putmpx->ut_type == USER_PROCESS)
00077                      users++;
00078 
00079        endutxent ();
00080 
00081        /* check the user count against warning and critical thresholds */
00082        if (users > cusers)
00083               result = STATE_CRITICAL;
00084        else if (users > wusers)
00085               result = STATE_WARNING;
00086        else if (users >= 0)
00087               result = STATE_OK;
00088 
00089        if (result == STATE_UNKNOWN)
00090               printf ("%s\n", _("Unable to read output"));
00091        else {
00092               asprintf (&perf, "%s", perfdata ("users", users, "",
00093                 TRUE, wusers,
00094                 TRUE, cusers,
00095                 TRUE, 0,
00096                 FALSE, 0));
00097               printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result),
00098                 users, perf);
00099        }
00100 
00101        return result;
00102 }
00103 
00104 /* process command-line arguments */
00105 int
00106 process_arguments (int argc, char **argv)
00107 {
00108        int c;
00109        int option = 0;
00110        static struct option longopts[] = {
00111               {"critical", required_argument, 0, 'c'},
00112               {"warning", required_argument, 0, 'w'},
00113               {"version", no_argument, 0, 'V'},
00114               {"help", no_argument, 0, 'h'},
00115               {0, 0, 0, 0}
00116        };
00117 
00118        if (argc < 2)
00119               usage ("\n");
00120 
00121        while (1) {
00122               c = getopt_long (argc, argv, "+hVvc:w:", longopts, &option);
00123 
00124               if (c == -1 || c == EOF || c == 1)
00125                      break;
00126 
00127               switch (c) {
00128               case '?':                                                             /* print short usage statement if args not parsable */
00129                      usage5 ();
00130               case 'h':                                                             /* help */
00131                      print_help ();
00132                      exit (STATE_OK);
00133               case 'V':                                                             /* version */
00134                      print_revision (progname, NP_VERSION);
00135                      exit (STATE_OK);
00136               case 'c':                                                             /* critical */
00137                      if (!is_intnonneg (optarg))
00138                             usage4 (_("Critical threshold must be a positive integer"));
00139                      else
00140                             cusers = atoi (optarg);
00141                      break;
00142               case 'w':                                                             /* warning */
00143                      if (!is_intnonneg (optarg))
00144                             usage4 (_("Warning threshold must be a positive integer"));
00145                      else
00146                             wusers = atoi (optarg);
00147                      break;
00148               }
00149        }
00150 
00151        c = optind;
00152        if (wusers == -1 && argc > c) {
00153               if (is_intnonneg (argv[c]) == FALSE)
00154                      usage4 (_("Warning threshold must be a positive integer"));
00155               else
00156                      wusers = atoi (argv[c++]);
00157        }
00158        if (cusers == -1 && argc > c) {
00159               if (is_intnonneg (argv[c]) == FALSE)
00160                      usage4 (_("Warning threshold must be a positive integer"));
00161               else
00162                      cusers = atoi (argv[c]);
00163        }
00164 
00165        return OK;
00166 }
00167 
00168 void
00169 print_help (void)
00170 {
00171        print_revision (progname, NP_VERSION);
00172 
00173        printf ("Copyright (c) 1999 Ethan Galstad\n");
00174        printf (COPYRIGHT, copyright, email);
00175 
00176        printf ("%s\n", _("This plugin checks the number of users currently logged in on the local"));
00177        printf ("%s\n", _("system and generates an error if the number exceeds the thresholds specified."));
00178 
00179        printf ("\n\n");
00180 
00181        print_usage ();
00182 
00183        printf (UT_HELP_VRSN);
00184        printf (UT_EXTRA_OPTS);
00185 
00186        printf (" %s\n", "-w, --warning=INTEGER");
00187        printf ("    %s\n", _("Set WARNING status if more than INTEGER users are logged in"));
00188        printf (" %s\n", "-c, --critical=INTEGER");
00189        printf ("    %s\n", _("Set CRITICAL status if more than INTEGER users are logged in"));
00190 
00191        printf (UT_SUPPORT);
00192 }
00193 
00194 void
00195 print_usage (void)
00196 {
00197        printf ("%s\n", _("Usage:"));
00198        printf ("%s -w <users> -c <users>\n", progname);
00199 }