4
0

utils_disk.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*****************************************************************************
  2. *
  3. * Library for check_disk
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2007 Nagios Plugins Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains utilities for check_disk. These are tested by libtap
  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. *****************************************************************************/
  28. #include "common.h"
  29. #include "utils_disk.h"
  30. void
  31. np_add_name (struct name_list **list, const char *name)
  32. {
  33. struct name_list *new_entry;
  34. new_entry = (struct name_list *) malloc (sizeof *new_entry);
  35. new_entry->name = (char *) name;
  36. new_entry->next = *list;
  37. *list = new_entry;
  38. }
  39. /* Initialises a new parameter at the end of list */
  40. struct parameter_list *
  41. np_add_parameter(struct parameter_list **list, const char *name)
  42. {
  43. struct parameter_list *current = *list;
  44. struct parameter_list *new_path;
  45. new_path = (struct parameter_list *) malloc (sizeof *new_path);
  46. new_path->name = (char *) name;
  47. new_path->best_match = NULL;
  48. new_path->name_next = NULL;
  49. new_path->freespace_bytes = NULL;
  50. new_path->freespace_units = NULL;
  51. new_path->freespace_percent = NULL;
  52. new_path->usedspace_bytes = NULL;
  53. new_path->usedspace_units = NULL;
  54. new_path->usedspace_percent = NULL;
  55. new_path->usedinodes_percent = NULL;
  56. new_path->freeinodes_percent = NULL;
  57. new_path->group = NULL;
  58. if (current == NULL) {
  59. *list = new_path;
  60. } else {
  61. while (current->name_next) {
  62. current = current->name_next;
  63. }
  64. current->name_next = new_path;
  65. }
  66. return new_path;
  67. }
  68. /* Delete a given parameter from list and return pointer to next element*/
  69. struct parameter_list *
  70. np_del_parameter(struct parameter_list *item, struct parameter_list *prev)
  71. {
  72. struct parameter_list *next;
  73. if (item->name_next)
  74. next = item->name_next;
  75. else
  76. next = NULL;
  77. free(item);
  78. if (prev)
  79. prev->name_next = next;
  80. return next;
  81. }
  82. /* returns a pointer to the struct found in the list */
  83. struct parameter_list *
  84. np_find_parameter(struct parameter_list *list, const char *name)
  85. {
  86. struct parameter_list *temp_list;
  87. for (temp_list = list; temp_list; temp_list = temp_list->name_next) {
  88. if (! strcmp(temp_list->name, name))
  89. return temp_list;
  90. }
  91. return NULL;
  92. }
  93. void
  94. np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, int exact)
  95. {
  96. struct parameter_list *d;
  97. for (d = desired; d; d= d->name_next) {
  98. if (! d->best_match) {
  99. struct mount_entry *me;
  100. size_t name_len = strlen(d->name);
  101. size_t best_match_len = 0;
  102. struct mount_entry *best_match = NULL;
  103. /* set best match if path name exactly matches a mounted device name */
  104. for (me = mount_list; me; me = me->me_next) {
  105. if (strcmp(me->me_devname, d->name)==0)
  106. best_match = me;
  107. }
  108. /* set best match by directory name if no match was found by devname */
  109. if (! best_match) {
  110. for (me = mount_list; me; me = me->me_next) {
  111. size_t len = strlen (me->me_mountdir);
  112. if ((exact == FALSE && (best_match_len <= len && len <= name_len &&
  113. (len == 1 || strncmp (me->me_mountdir, d->name, len) == 0)))
  114. || (exact == TRUE && strcmp(me->me_mountdir, d->name)==0))
  115. {
  116. best_match = me;
  117. best_match_len = len;
  118. }
  119. }
  120. }
  121. if (best_match) {
  122. d->best_match = best_match;
  123. } else {
  124. d->best_match = NULL; /* Not sure why this is needed as it should be null on initialisation */
  125. }
  126. }
  127. }
  128. }
  129. /* Returns TRUE if name is in list */
  130. int
  131. np_find_name (struct name_list *list, const char *name)
  132. {
  133. const struct name_list *n;
  134. if (list == NULL || name == NULL) {
  135. return FALSE;
  136. }
  137. for (n = list; n; n = n->next) {
  138. if (!strcmp(name, n->name)) {
  139. return TRUE;
  140. }
  141. }
  142. return FALSE;
  143. }
  144. int
  145. np_seen_name(struct name_list *list, const char *name)
  146. {
  147. const struct name_list *s;
  148. for (s = list; s; s=s->next) {
  149. if (!strcmp(s->name, name)) {
  150. return TRUE;
  151. }
  152. }
  153. return FALSE;
  154. }
  155. int
  156. np_regex_match_mount_entry (struct mount_entry* me, regex_t* re)
  157. {
  158. if (regexec(re, me->me_devname, (size_t) 0, NULL, 0) == 0 ||
  159. regexec(re, me->me_mountdir, (size_t) 0, NULL, 0) == 0 ) {
  160. return TRUE;
  161. } else {
  162. return FALSE;
  163. }
  164. }