1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* http://www.i-scream.org |
4 |
* Copyright (C) 2000-2003 i-scream |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
*/ |
20 |
|
21 |
#ifdef HAVE_CONFIG_H |
22 |
#include "config.h" |
23 |
#endif |
24 |
|
25 |
#include <stdio.h> |
26 |
#include <string.h> |
27 |
#include <stdlib.h> |
28 |
#include <sys/types.h> |
29 |
#include <regex.h> |
30 |
#ifdef ALLBSD |
31 |
#include <fcntl.h> |
32 |
#include <kvm.h> |
33 |
#endif |
34 |
#ifdef NETBSD |
35 |
#include <uvm/uvm_extern.h> |
36 |
#endif |
37 |
|
38 |
#include "tools.h" |
39 |
|
40 |
#ifdef SOLARIS |
41 |
#include <libdevinfo.h> |
42 |
#include <kstat.h> |
43 |
#include <unistd.h> |
44 |
#include <ctype.h> |
45 |
#include <sys/types.h> |
46 |
#include <sys/dkio.h> |
47 |
#include <sys/stat.h> |
48 |
#include <fcntl.h> |
49 |
#include <sys/fcntl.h> |
50 |
#include <dirent.h> |
51 |
#endif |
52 |
|
53 |
#ifdef SOLARIS |
54 |
struct map{ |
55 |
char *bsd; |
56 |
char *svr; |
57 |
|
58 |
struct map *next; |
59 |
}; |
60 |
typedef struct map mapping_t; |
61 |
|
62 |
static mapping_t *mapping = NULL; |
63 |
|
64 |
char *get_svr_from_bsd(char *bsd){ |
65 |
mapping_t *map_ptr; |
66 |
for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next) |
67 |
if(!strcmp(map_ptr->bsd, bsd)) return map_ptr->svr; |
68 |
|
69 |
return bsd; |
70 |
} |
71 |
|
72 |
void add_mapping(char *bsd, char *svr){ |
73 |
mapping_t *map_ptr; |
74 |
mapping_t *map_end_ptr; |
75 |
|
76 |
bsd = strdup(bsd); |
77 |
svr = strdup(svr); |
78 |
|
79 |
if (mapping == NULL){ |
80 |
printf("New malloc\n"); |
81 |
mapping = malloc(sizeof(mapping_t)); |
82 |
if (mapping == NULL) return; |
83 |
map_ptr = mapping; |
84 |
}else{ |
85 |
/* See if its already been added */ |
86 |
for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next){ |
87 |
if( (!strcmp(map_ptr->bsd, bsd)) || (!strcmp(map_ptr->svr, svr)) ){ |
88 |
printf("%s matches %s\n", map_ptr->bsd, bsd); |
89 |
return; |
90 |
} |
91 |
map_end_ptr = map_ptr; |
92 |
} |
93 |
|
94 |
/* We've reached end of list and not found the entry.. So we need to malloc |
95 |
* new mapping_t |
96 |
*/ |
97 |
map_end_ptr->next = malloc(sizeof(mapping_t)); |
98 |
printf("Second malloc\n"); |
99 |
if (map_end_ptr->next == NULL) return; |
100 |
map_ptr = map_end_ptr->next; |
101 |
} |
102 |
|
103 |
printf("Adding %s\n", bsd); |
104 |
map_ptr->next = NULL; |
105 |
map_ptr->bsd = bsd; |
106 |
map_ptr->svr = svr; |
107 |
|
108 |
return; |
109 |
} |
110 |
|
111 |
char *read_dir(char *disk_path){ |
112 |
DIR *dirp; |
113 |
struct dirent *dp; |
114 |
struct stat stbuf; |
115 |
char *svr_name; |
116 |
char current_dir[MAXPATHLEN]; |
117 |
char file_name[MAXPATHLEN]; |
118 |
char temp_name[MAXPATHLEN]; |
119 |
char dir_dname[MAXPATHLEN]; |
120 |
char *dsk_dir; |
121 |
int x; |
122 |
|
123 |
dsk_dir = "/dev/osa/dev/dsk"; |
124 |
strncpy(current_dir, dsk_dir, sizeof current_dir); |
125 |
if ((dirp = opendir(current_dir)) == NULL){ |
126 |
dsk_dir = "/dev/dsk"; |
127 |
snprintf(current_dir, sizeof current_dir, "%s", dsk_dir); |
128 |
if ((dirp = opendir(current_dir)) == NULL){ |
129 |
return NULL; |
130 |
} |
131 |
} |
132 |
|
133 |
while ((dp = readdir(dirp)) != NULL){ |
134 |
snprintf(temp_name, sizeof temp_name, "../..%s", disk_path); |
135 |
snprintf(dir_dname, sizeof dir_dname, "%s/%s", dsk_dir, dp->d_name); |
136 |
stat(dir_dname,&stbuf); |
137 |
|
138 |
if (S_ISBLK(stbuf.st_mode)){ |
139 |
x = readlink(dir_dname, file_name, sizeof(file_name)); |
140 |
file_name[x] = '\0'; |
141 |
if (strcmp(file_name, temp_name) == 0) { |
142 |
svr_name = strdup(dp->d_name); |
143 |
closedir(dirp); |
144 |
return svr_name; |
145 |
} |
146 |
} |
147 |
closedir(dirp); |
148 |
} |
149 |
return NULL; |
150 |
} |
151 |
|
152 |
|
153 |
|
154 |
int get_alias(char *alias){ |
155 |
char file[MAXPATHLEN]; |
156 |
di_node_t root_node; |
157 |
di_node_t node; |
158 |
di_minor_t minor = DI_MINOR_NIL; |
159 |
char tmpnode[MAXPATHLEN]; |
160 |
char *phys_path; |
161 |
char *minor_name; |
162 |
char *value; |
163 |
int instance; |
164 |
if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) { |
165 |
fprintf(stderr, "di_init() failed\n"); |
166 |
exit(1); |
167 |
} |
168 |
node = di_drv_first_node(alias, root_node); |
169 |
while (node != DI_NODE_NIL) { |
170 |
if ((minor = di_minor_next(node, DI_MINOR_NIL)) != DI_MINOR_NIL) { |
171 |
instance = di_instance(node); |
172 |
phys_path = di_devfs_path(node); |
173 |
minor_name = di_minor_name(minor); |
174 |
strcpy(tmpnode, alias); |
175 |
sprintf(tmpnode, "%s%d", tmpnode, instance); |
176 |
strcpy(file, "/devices"); |
177 |
strcat(file, phys_path); |
178 |
strcat(file, ":"); |
179 |
strcat(file, minor_name); |
180 |
value = read_dir(file); |
181 |
if (value != NULL){ |
182 |
add_mapping(tmpnode, value); |
183 |
} |
184 |
di_devfs_path_free(phys_path); |
185 |
node = di_drv_next_node(node); |
186 |
}else{ |
187 |
node = di_drv_next_node(node); |
188 |
} |
189 |
} |
190 |
di_fini(root_node); |
191 |
return (-1); |
192 |
} |
193 |
|
194 |
void build_mapping(){ |
195 |
char device_name[512]; |
196 |
int x; |
197 |
kstat_ctl_t *kc; |
198 |
kstat_t *ksp; |
199 |
kstat_io_t kios; |
200 |
|
201 |
if ((kc = kstat_open()) == NULL) { |
202 |
return NULL; |
203 |
} |
204 |
|
205 |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
206 |
if (!strcmp(ksp->ks_class, "disk")) { |
207 |
if(ksp->ks_type != KSTAT_TYPE_IO) continue; |
208 |
/* We dont want metadevices appearing as num_diskio */ |
209 |
if(strcmp(ksp->ks_module, "md")==0) continue; |
210 |
if((kstat_read(kc, ksp, &kios))==-1) continue; |
211 |
strncpy(device_name, ksp->ks_name, sizeof device_name); |
212 |
for(x=0;x<(sizeof device_name);x++){ |
213 |
if( isdigit((int)device_name[x]) ) break; |
214 |
} |
215 |
if(x == sizeof device_name) x--; |
216 |
device_name[x] = '\0'; |
217 |
get_alias(device_name); |
218 |
} |
219 |
} |
220 |
|
221 |
return; |
222 |
} |
223 |
|
224 |
#endif |
225 |
|
226 |
|
227 |
|
228 |
char *f_read_line(FILE *f, const char *string){ |
229 |
/* Max line length. 8k should be more than enough */ |
230 |
static char line[8192]; |
231 |
|
232 |
while((fgets(line, sizeof(line), f))!=NULL){ |
233 |
if(strncmp(string, line, strlen(string))==0){ |
234 |
return line; |
235 |
} |
236 |
} |
237 |
|
238 |
return NULL; |
239 |
} |
240 |
|
241 |
char *get_string_match(char *line, regmatch_t *match){ |
242 |
int len=match->rm_eo - match->rm_so; |
243 |
char *match_string=malloc(len+1); |
244 |
|
245 |
match_string=strncpy(match_string, line+match->rm_so, len); |
246 |
match_string[len]='\0'; |
247 |
|
248 |
return match_string; |
249 |
} |
250 |
|
251 |
|
252 |
|
253 |
#ifndef HAVE_ATOLL |
254 |
static long long atoll(const char *s) { |
255 |
long long value = 0; |
256 |
int isneg = 0; |
257 |
|
258 |
while (*s == ' ' || *s == '\t') { |
259 |
s++; |
260 |
} |
261 |
if (*s == '-') { |
262 |
isneg = 1; |
263 |
s++; |
264 |
} |
265 |
while (*s >= '0' && *s <= '9') { |
266 |
value = (10 * value) + (*s - '0'); |
267 |
s++; |
268 |
} |
269 |
return (isneg ? -value : value); |
270 |
} |
271 |
#endif |
272 |
|
273 |
long long get_ll_match(char *line, regmatch_t *match){ |
274 |
char *ptr; |
275 |
long long num; |
276 |
|
277 |
ptr=line+match->rm_so; |
278 |
num=atoll(ptr); |
279 |
|
280 |
return num; |
281 |
} |
282 |
|
283 |
#ifdef ALLBSD |
284 |
kvm_t *get_kvm() { |
285 |
static kvm_t *kvmd = NULL; |
286 |
|
287 |
if (kvmd != NULL) { |
288 |
return kvmd; |
289 |
} |
290 |
|
291 |
kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL); |
292 |
return kvmd; |
293 |
} |
294 |
#endif |
295 |
|
296 |
#ifdef NETBSD |
297 |
struct uvmexp *get_uvmexp() { |
298 |
static u_long addr = 0; |
299 |
static struct uvmexp uvm; |
300 |
kvm_t *kvmd = get_kvm(); |
301 |
|
302 |
if (kvmd == NULL) { |
303 |
return NULL; |
304 |
} |
305 |
|
306 |
if (addr == 0) { |
307 |
struct nlist symbols[] = { |
308 |
{ "_uvmexp" }, |
309 |
{ NULL } |
310 |
}; |
311 |
|
312 |
if (kvm_nlist(kvmd, symbols) != 0) { |
313 |
return NULL; |
314 |
} |
315 |
addr = symbols[0].n_value; |
316 |
} |
317 |
|
318 |
if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) { |
319 |
return NULL; |
320 |
} |
321 |
return &uvm; |
322 |
} |
323 |
#endif |
324 |
|
325 |
int statgrab_init(){ |
326 |
#ifdef ALLBSD |
327 |
{ |
328 |
kvm_t *kvmd = get_kvm(); |
329 |
if (kvmd == NULL) return 1; |
330 |
} |
331 |
#endif |
332 |
#ifdef NETBSD |
333 |
{ |
334 |
struct uvmexp *uvm = get_uvmexp(); |
335 |
if (uvm == NULL) return 1; |
336 |
} |
337 |
#endif |
338 |
#ifdef SOLARIS |
339 |
build_mapping(); |
340 |
#endif |
341 |
return 0; |
342 |
} |