pkg-monitoring-plugins/lib/utils_disk.c

271 lines
7.2 KiB
C
Raw Normal View History

2013-11-26 22:55:28 +00:00
/*****************************************************************************
2023-10-18 07:29:37 +00:00
*
2013-11-26 22:55:28 +00:00
* Library for check_disk
2023-10-18 07:29:37 +00:00
*
2013-11-26 22:53:19 +00:00
* License: GPL
2014-07-11 19:01:00 +00:00
* Copyright (c) 1999-2007 Monitoring Plugins Development Team
2023-10-18 07:29:37 +00:00
*
2013-11-26 22:53:19 +00:00
* Description:
2023-10-18 07:29:37 +00:00
*
2013-11-26 22:53:19 +00:00
* This file contains utilities for check_disk. These are tested by libtap
2023-10-18 07:29:37 +00:00
*
*
2013-11-26 22:55:28 +00:00
* This program is free software: you can redistribute it and/or modify
2013-11-26 22:53:19 +00:00
* it under the terms of the GNU General Public License as published by
2013-11-26 22:55:28 +00:00
* the Free Software Foundation, either version 3 of the License, or
2013-11-26 22:53:19 +00:00
* (at your option) any later version.
2023-10-18 07:29:37 +00:00
*
2013-11-26 22:53:19 +00:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
2023-10-18 07:29:37 +00:00
*
2013-11-26 22:53:19 +00:00
* You should have received a copy of the GNU General Public License
2013-11-26 22:55:28 +00:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2023-10-18 07:29:37 +00:00
*
*
2013-11-26 22:53:19 +00:00
*****************************************************************************/
#include "common.h"
#include "utils_disk.h"
2023-02-02 09:13:25 +00:00
#include "gl/fsusage.h"
2023-10-18 07:29:37 +00:00
#include <string.h>
2013-11-26 22:53:19 +00:00
void
np_add_name (struct name_list **list, const char *name)
{
struct name_list *new_entry;
new_entry = (struct name_list *) malloc (sizeof *new_entry);
new_entry->name = (char *) name;
new_entry->next = *list;
*list = new_entry;
}
2023-10-18 07:29:37 +00:00
/* @brief Initialises a new regex at the begin of list via regcomp(3)
*
* @details if the regex fails to compile the error code of regcomp(3) is returned
* and list is not modified, otherwise list is modified to point to the new
* element
* @param list Pointer to a linked list of regex_list elements
* @param regex the string containing the regex which should be inserted into the list
* @param clags the cflags parameter for regcomp(3)
*/
int
np_add_regex (struct regex_list **list, const char *regex, int cflags)
{
struct regex_list *new_entry = (struct regex_list *) malloc (sizeof *new_entry);
if (new_entry == NULL) {
die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
strerror(errno));
}
int regcomp_result = regcomp(&new_entry->regex, regex, cflags);
if (!regcomp_result) {
// regcomp succeeded
new_entry->next = *list;
*list = new_entry;
return 0;
} else {
// regcomp failed
free(new_entry);
return regcomp_result;
}
}
2013-11-26 22:53:19 +00:00
/* Initialises a new parameter at the end of list */
struct parameter_list *
np_add_parameter(struct parameter_list **list, const char *name)
{
struct parameter_list *current = *list;
struct parameter_list *new_path;
new_path = (struct parameter_list *) malloc (sizeof *new_path);
2023-10-18 07:29:37 +00:00
new_path->name = (char *) malloc(strlen(name) + 1);
2013-11-26 22:53:19 +00:00
new_path->best_match = NULL;
new_path->name_next = NULL;
2023-10-18 07:29:37 +00:00
new_path->name_prev = NULL;
2013-11-26 22:53:19 +00:00
new_path->freespace_bytes = NULL;
new_path->freespace_units = NULL;
new_path->freespace_percent = NULL;
new_path->usedspace_bytes = NULL;
new_path->usedspace_units = NULL;
new_path->usedspace_percent = NULL;
new_path->usedinodes_percent = NULL;
2013-11-26 22:53:44 +00:00
new_path->freeinodes_percent = NULL;
new_path->group = NULL;
2013-11-26 22:58:53 +00:00
new_path->dfree_pct = -1;
2023-10-18 07:29:37 +00:00
new_path->dused_pct = -1;
2013-11-26 22:58:53 +00:00
new_path->total = 0;
new_path->available = 0;
new_path->available_to_root = 0;
new_path->used = 0;
new_path->dused_units = 0;
new_path->dfree_units = 0;
new_path->dtotal_units = 0;
new_path->inodes_total = 0;
new_path->inodes_free = 0;
2020-12-10 20:00:09 +00:00
new_path->inodes_free_to_root = 0;
new_path->inodes_used = 0;
2013-11-26 22:58:53 +00:00
new_path->dused_inodes_percent = 0;
new_path->dfree_inodes_percent = 0;
2013-11-26 22:53:19 +00:00
2023-10-18 07:29:37 +00:00
strcpy(new_path->name, name);
2013-11-26 22:53:19 +00:00
if (current == NULL) {
*list = new_path;
2023-10-18 07:29:37 +00:00
new_path->name_prev = NULL;
2013-11-26 22:53:19 +00:00
} else {
while (current->name_next) {
current = current->name_next;
}
current->name_next = new_path;
2023-10-18 07:29:37 +00:00
new_path->name_prev = current;
2013-11-26 22:53:19 +00:00
}
return new_path;
}
2013-11-26 22:54:42 +00:00
/* Delete a given parameter from list and return pointer to next element*/
struct parameter_list *
np_del_parameter(struct parameter_list *item, struct parameter_list *prev)
{
2023-10-18 07:29:37 +00:00
if (item == NULL) {
return NULL;
}
struct parameter_list *next;
2013-11-26 22:54:42 +00:00
if (item->name_next)
next = item->name_next;
else
next = NULL;
2013-11-26 22:54:42 +00:00
2023-10-18 07:29:37 +00:00
if (next)
next->name_prev = prev;
if (prev)
prev->name_next = next;
2013-11-26 22:54:42 +00:00
2023-10-18 07:29:37 +00:00
if (item->name) {
free(item->name);
}
free(item);
return next;
2013-11-26 22:54:42 +00:00
}
2013-11-26 22:53:44 +00:00
/* returns a pointer to the struct found in the list */
struct parameter_list *
np_find_parameter(struct parameter_list *list, const char *name)
{
struct parameter_list *temp_list;
for (temp_list = list; temp_list; temp_list = temp_list->name_next) {
if (! strcmp(temp_list->name, name))
return temp_list;
}
2013-11-26 22:53:44 +00:00
return NULL;
}
2023-10-18 07:29:37 +00:00
void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact) {
2013-11-26 22:53:19 +00:00
struct parameter_list *d;
for (d = desired; d; d= d->name_next) {
2013-11-26 22:53:44 +00:00
if (! d->best_match) {
struct mount_entry *me;
size_t name_len = strlen(d->name);
size_t best_match_len = 0;
struct mount_entry *best_match = NULL;
2023-02-02 09:13:25 +00:00
struct fs_usage fsp;
2013-11-26 22:53:19 +00:00
2013-11-26 22:53:44 +00:00
/* set best match if path name exactly matches a mounted device name */
for (me = mount_list; me; me = me->me_next) {
2023-10-18 07:29:37 +00:00
if (strcmp(me->me_devname, d->name)==0) {
if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) {
best_match = me;
}
}
2013-11-26 22:53:44 +00:00
}
/* set best match by directory name if no match was found by devname */
if (! best_match) {
for (me = mount_list; me; me = me->me_next) {
size_t len = strlen (me->me_mountdir);
2023-10-18 07:29:37 +00:00
if ((!exact && (best_match_len <= len && len <= name_len &&
2013-11-26 22:53:44 +00:00
(len == 1 || strncmp (me->me_mountdir, d->name, len) == 0)))
2023-10-18 07:29:37 +00:00
|| (exact && strcmp(me->me_mountdir, d->name)==0))
2013-11-26 22:53:44 +00:00
{
2023-10-18 07:29:37 +00:00
if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) {
best_match = me;
best_match_len = len;
}
2013-11-26 22:53:44 +00:00
}
2013-11-26 22:53:19 +00:00
}
}
2013-11-26 22:53:44 +00:00
if (best_match) {
d->best_match = best_match;
} else {
d->best_match = NULL; /* Not sure why this is needed as it should be null on initialisation */
}
2013-11-26 22:53:19 +00:00
}
}
}
2023-10-18 07:29:37 +00:00
/* Returns true if name is in list */
bool np_find_name (struct name_list *list, const char *name) {
2013-11-26 22:53:19 +00:00
const struct name_list *n;
if (list == NULL || name == NULL) {
2023-10-18 07:29:37 +00:00
return false;
2013-11-26 22:53:19 +00:00
}
for (n = list; n; n = n->next) {
if (!strcmp(name, n->name)) {
2023-10-18 07:29:37 +00:00
return true;
2013-11-26 22:53:19 +00:00
}
}
2023-10-18 07:29:37 +00:00
return false;
2013-11-26 22:53:19 +00:00
}
2023-10-18 07:29:37 +00:00
/* Returns true if name is in list */
bool np_find_regmatch (struct regex_list *list, const char *name) {
int len;
regmatch_t m;
if (name == NULL) {
return false;
}
len = strlen(name);
for (; list; list = list->next) {
/* Emulate a full match as if surrounded with ^( )$
by checking whether the match spans the whole name */
if (!regexec(&list->regex, name, 1, &m, 0) && m.rm_so == 0 && m.rm_eo == len) {
return true;
}
}
return false;
}
bool np_seen_name(struct name_list *list, const char *name) {
2013-11-26 22:53:19 +00:00
const struct name_list *s;
for (s = list; s; s=s->next) {
if (!strcmp(s->name, name)) {
2023-10-18 07:29:37 +00:00
return true;
2013-11-26 22:53:19 +00:00
}
}
2023-10-18 07:29:37 +00:00
return false;
2013-11-26 22:53:19 +00:00
}
2023-10-18 07:29:37 +00:00
bool np_regex_match_mount_entry (struct mount_entry* me, regex_t* re) {
2013-11-26 22:53:44 +00:00
if (regexec(re, me->me_devname, (size_t) 0, NULL, 0) == 0 ||
regexec(re, me->me_mountdir, (size_t) 0, NULL, 0) == 0 ) {
2023-10-18 07:29:37 +00:00
return true;
2013-11-26 22:53:44 +00:00
}
2023-10-18 07:29:37 +00:00
return false;
2013-11-26 22:53:44 +00:00
}