utils_disk.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*****************************************************************************
  2. *
  3. * Library for check_disk
  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 utilities for check_disk. These are tested by libtap
  13. *
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. * $Id$
  29. *
  30. *****************************************************************************/
  31. #include "common.h"
  32. #include "utils_disk.h"
  33. void
  34. np_add_name (struct name_list **list, const char *name)
  35. {
  36. struct name_list *new_entry;
  37. new_entry = (struct name_list *) malloc (sizeof *new_entry);
  38. new_entry->name = (char *) name;
  39. new_entry->next = *list;
  40. *list = new_entry;
  41. }
  42. /* Initialises a new parameter at the end of list */
  43. struct parameter_list *
  44. np_add_parameter(struct parameter_list **list, const char *name)
  45. {
  46. struct parameter_list *current = *list;
  47. struct parameter_list *new_path;
  48. new_path = (struct parameter_list *) malloc (sizeof *new_path);
  49. new_path->name = (char *) name;
  50. new_path->best_match = NULL;
  51. new_path->name_next = NULL;
  52. new_path->freespace_bytes = NULL;
  53. new_path->freespace_units = NULL;
  54. new_path->freespace_percent = NULL;
  55. new_path->usedspace_bytes = NULL;
  56. new_path->usedspace_units = NULL;
  57. new_path->usedspace_percent = NULL;
  58. new_path->usedinodes_percent = NULL;
  59. new_path->freeinodes_percent = NULL;
  60. new_path->group = NULL;
  61. if (current == NULL) {
  62. *list = new_path;
  63. } else {
  64. while (current->name_next) {
  65. current = current->name_next;
  66. }
  67. current->name_next = new_path;
  68. }
  69. return new_path;
  70. }
  71. /* Delete a given parameter from list and return pointer to next element*/
  72. struct parameter_list *
  73. np_del_parameter(struct parameter_list *item, struct parameter_list *prev)
  74. {
  75. struct parameter_list *next;
  76. if (item->name_next)
  77. next = item->name_next;
  78. else
  79. next = NULL;
  80. free(item);
  81. if (prev)
  82. prev->name_next = next;
  83. return next;
  84. }
  85. /* returns a pointer to the struct found in the list */
  86. struct parameter_list *
  87. np_find_parameter(struct parameter_list *list, const char *name)
  88. {
  89. struct parameter_list *temp_list;
  90. for (temp_list = list; temp_list; temp_list = temp_list->name_next) {
  91. if (! strcmp(temp_list->name, name))
  92. return temp_list;
  93. }
  94. return NULL;
  95. }
  96. void
  97. np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, int exact)
  98. {
  99. struct parameter_list *d;
  100. for (d = desired; d; d= d->name_next) {
  101. if (! d->best_match) {
  102. struct mount_entry *me;
  103. size_t name_len = strlen(d->name);
  104. size_t best_match_len = 0;
  105. struct mount_entry *best_match = NULL;
  106. /* set best match if path name exactly matches a mounted device name */
  107. for (me = mount_list; me; me = me->me_next) {
  108. if (strcmp(me->me_devname, d->name)==0)
  109. best_match = me;
  110. }
  111. /* set best match by directory name if no match was found by devname */
  112. if (! best_match) {
  113. for (me = mount_list; me; me = me->me_next) {
  114. size_t len = strlen (me->me_mountdir);
  115. if ((exact == FALSE && (best_match_len <= len && len <= name_len &&
  116. (len == 1 || strncmp (me->me_mountdir, d->name, len) == 0)))
  117. || (exact == TRUE && strcmp(me->me_mountdir, d->name)==0))
  118. {
  119. best_match = me;
  120. best_match_len = len;
  121. }
  122. }
  123. }
  124. if (best_match) {
  125. d->best_match = best_match;
  126. } else {
  127. d->best_match = NULL; /* Not sure why this is needed as it should be null on initialisation */
  128. }
  129. }
  130. }
  131. }
  132. /* Returns TRUE if name is in list */
  133. int
  134. np_find_name (struct name_list *list, const char *name)
  135. {
  136. const struct name_list *n;
  137. if (list == NULL || name == NULL) {
  138. return FALSE;
  139. }
  140. for (n = list; n; n = n->next) {
  141. if (!strcmp(name, n->name)) {
  142. return TRUE;
  143. }
  144. }
  145. return FALSE;
  146. }
  147. int
  148. np_seen_name(struct name_list *list, const char *name)
  149. {
  150. const struct name_list *s;
  151. for (s = list; s; s=s->next) {
  152. if (!strcmp(s->name, name)) {
  153. return TRUE;
  154. }
  155. }
  156. return FALSE;
  157. }
  158. int
  159. np_regex_match_mount_entry (struct mount_entry* me, regex_t* re)
  160. {
  161. if (regexec(re, me->me_devname, (size_t) 0, NULL, 0) == 0 ||
  162. regexec(re, me->me_mountdir, (size_t) 0, NULL, 0) == 0 ) {
  163. return TRUE;
  164. } else {
  165. return FALSE;
  166. }
  167. }