utils_disk.c 4.9 KB

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