Back to index

nagios-plugins  1.4.16
utils_disk.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * Library for check_disk
00004 * 
00005 * License: GPL
00006 * Copyright (c) 1999-2007 Nagios Plugins Development Team
00007 * 
00008 * Description:
00009 * 
00010 * This file contains utilities for check_disk. These are tested by libtap
00011 * 
00012 * 
00013 * This program is free software: you can redistribute it and/or modify
00014 * it under the terms of the GNU General Public License as published by
00015 * the Free Software Foundation, either version 3 of the License, or
00016 * (at your option) any later version.
00017 * 
00018 * This program is distributed in the hope that it will be useful,
00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021 * GNU General Public License for more details.
00022 * 
00023 * You should have received a copy of the GNU General Public License
00024 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025 * 
00026 * 
00027 *****************************************************************************/
00028 
00029 #include "common.h"
00030 #include "utils_disk.h"
00031 
00032 void
00033 np_add_name (struct name_list **list, const char *name)
00034 {
00035   struct name_list *new_entry;
00036   new_entry = (struct name_list *) malloc (sizeof *new_entry);
00037   new_entry->name = (char *) name;
00038   new_entry->next = *list;
00039   *list = new_entry;
00040 }
00041 
00042 /* Initialises a new parameter at the end of list */
00043 struct parameter_list *
00044 np_add_parameter(struct parameter_list **list, const char *name)
00045 {
00046   struct parameter_list *current = *list;
00047   struct parameter_list *new_path;
00048   new_path = (struct parameter_list *) malloc (sizeof *new_path);
00049   new_path->name = (char *) name;
00050   new_path->best_match = NULL;
00051   new_path->name_next = NULL;
00052   new_path->freespace_bytes = NULL;
00053   new_path->freespace_units = NULL;
00054   new_path->freespace_percent = NULL;
00055   new_path->usedspace_bytes = NULL;
00056   new_path->usedspace_units = NULL;
00057   new_path->usedspace_percent = NULL;
00058   new_path->usedinodes_percent = NULL;
00059   new_path->freeinodes_percent = NULL;
00060   new_path->group = NULL;
00061   new_path->dfree_pct = -1;
00062   new_path->dused_pct = -1; 
00063   new_path->total = 0;
00064   new_path->available = 0;
00065   new_path->available_to_root = 0;
00066   new_path->used = 0;
00067   new_path->dused_units = 0;
00068   new_path->dfree_units = 0;
00069   new_path->dtotal_units = 0;
00070   new_path->inodes_total = 0;
00071   new_path->inodes_free = 0;
00072   new_path->dused_inodes_percent = 0;
00073   new_path->dfree_inodes_percent = 0;
00074 
00075   if (current == NULL) {
00076     *list = new_path;
00077   } else {
00078     while (current->name_next) {
00079       current = current->name_next;
00080     }
00081     current->name_next = new_path;
00082   }
00083   return new_path;
00084 }
00085 
00086 /* Delete a given parameter from list and return pointer to next element*/
00087 struct parameter_list *
00088 np_del_parameter(struct parameter_list *item, struct parameter_list *prev)
00089 {
00090   struct parameter_list *next;
00091 
00092   if (item->name_next)
00093     next = item->name_next;
00094   else
00095     next = NULL;
00096 
00097   free(item);
00098   if (prev)
00099     prev->name_next = next;
00100 
00101   return next;
00102 }
00103 
00104 
00105 /* returns a pointer to the struct found in the list */
00106 struct parameter_list *
00107 np_find_parameter(struct parameter_list *list, const char *name)
00108 {
00109   struct parameter_list *temp_list;
00110   for (temp_list = list; temp_list; temp_list = temp_list->name_next) {
00111     if (! strcmp(temp_list->name, name))
00112         return temp_list;
00113   }
00114 
00115   return NULL;
00116 }
00117 
00118 void
00119 np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, int exact)
00120 {
00121   struct parameter_list *d;
00122   for (d = desired; d; d= d->name_next) {
00123     if (! d->best_match) {
00124       struct mount_entry *me;
00125       size_t name_len = strlen(d->name);
00126       size_t best_match_len = 0;
00127       struct mount_entry *best_match = NULL;
00128 
00129       /* set best match if path name exactly matches a mounted device name */
00130       for (me = mount_list; me; me = me->me_next) {
00131         if (strcmp(me->me_devname, d->name)==0)
00132           best_match = me;
00133       }
00134 
00135       /* set best match by directory name if no match was found by devname */
00136       if (! best_match) {
00137         for (me = mount_list; me; me = me->me_next) {
00138           size_t len = strlen (me->me_mountdir);
00139           if ((exact == FALSE && (best_match_len <= len && len <= name_len &&
00140              (len == 1 || strncmp (me->me_mountdir, d->name, len) == 0)))
00141              || (exact == TRUE && strcmp(me->me_mountdir, d->name)==0))
00142           {
00143             best_match = me;
00144             best_match_len = len;
00145           }
00146         }
00147       }
00148 
00149       if (best_match) {
00150         d->best_match = best_match;
00151       } else {
00152         d->best_match = NULL;      /* Not sure why this is needed as it should be null on initialisation */
00153       }
00154     }
00155   }
00156 }
00157 
00158 /* Returns TRUE if name is in list */
00159 int
00160 np_find_name (struct name_list *list, const char *name)
00161 {
00162   const struct name_list *n;
00163 
00164   if (list == NULL || name == NULL) {
00165     return FALSE;
00166   }
00167   for (n = list; n; n = n->next) {
00168     if (!strcmp(name, n->name)) {
00169       return TRUE;
00170     }
00171   }
00172   return FALSE;
00173 }
00174 
00175 int
00176 np_seen_name(struct name_list *list, const char *name)
00177 {
00178   const struct name_list *s;
00179   for (s = list; s; s=s->next) {
00180     if (!strcmp(s->name, name)) {
00181       return TRUE;
00182     }
00183   }
00184   return FALSE;
00185 }
00186 
00187 int
00188 np_regex_match_mount_entry (struct mount_entry* me, regex_t* re)
00189 {
00190   if (regexec(re, me->me_devname, (size_t) 0, NULL, 0) == 0 ||
00191       regexec(re, me->me_mountdir, (size_t) 0, NULL, 0) == 0 ) {
00192     return TRUE;
00193   } else {
00194     return FALSE;
00195   }
00196 }
00197