1 /*----------------------------------------------------------------------------*/
2 /* Xymon RRD handler module. */
3 /* */
4 /* Copyright (C) 2004-2006 Francesco Duranti <fduranti@q8.it> */
5 /* */
6 /* This program is released under the GNU General Public License (GPL), */
7 /* version 2. See the file "COPYING" for details. */
8 /* */
9 /*----------------------------------------------------------------------------*/
10
11 /*
12 Some function used by do_netapp.pl do_beastat.pl and do_dbcheck.pl
13 */
14
15 static char fd_lib_rcs[] = "$Id: do_fd_lib.c 6648 2011-03-08 13:05:32Z storner $";
16
get_kb_data(char * msg,char * search)17 unsigned long get_kb_data(char *msg,char *search)
18 {
19 char *p,*r,*eoln;
20 unsigned long value=0;
21
22 /* go to the start of the message */
23 p = strstr(msg, search);
24 if (p) {
25 /* set the endofline */
26 eoln = strchr(p, '\n'); if (eoln) *eoln = '\0';
27
28 /* search for a ":" or "=" */
29 if ((r = strchr(p,':')) == NULL) r=strchr(p,'=');
30 // dbgprintf("1getdata %s\n",p);
31 // dbgprintf("2getdata %s\n",r);
32 if (r) {
33 r++;
34 value=atol(r);
35 /* Convert to KB if there's a modifier after the numbers */
36 r += strspn(r, "0123456789. ");
37 if (*r == 'M') value *= 1024;
38 else if (*r == 'G') value *= (1024*1024);
39 else if (*r == 'T') value *= (1024*1024*1024);
40 }
41 /* reset the endofline */
42 if (eoln) *eoln = '\n';
43 }
44 return value;
45 }
46
get_long_data(char * msg,char * search)47 unsigned long get_long_data(char *msg,char *search)
48 {
49 char *p,*r,*eoln;
50 unsigned long value=0;
51
52 /* go to the start of the message */
53 p = strstr(msg, search);
54 if (p) {
55 /* set the endofline */
56 eoln = strchr(p, '\n'); if (eoln) *eoln = '\0';
57
58 /* search for a ":" or "=" */
59 if ((r = strchr(p, ':')) == NULL) r=strchr(p, '=');
60 // dbgprintf("1getdata %s\n",p);
61 // dbgprintf("2getdata %s\n",r);
62 if (r) {
63 value=atol(++r);
64 }
65 /* reset the endofline */
66 if (eoln) *eoln = '\n';
67 }
68 return value;
69 }
70
get_double_data(char * msg,char * search)71 double get_double_data(char *msg,char *search)
72 {
73 char *p,*r,*eoln;
74 double value=0;
75
76 /* go to the start of the message */
77 p = strstr(msg, search);
78 if (p) {
79 /* set the endofline */
80 eoln = strchr(p, '\n'); if (eoln) *eoln = '\0';
81
82 /* search for a ":" or "=" */
83 if ((r = strchr(p,':')) == NULL) r=strchr(p,'=');
84 // dbgprintf("1getdata %s\n",p);
85 // dbgprintf("2getdata %s\n",r);
86 if (r) value=atof(++r);
87 /* reset the endofline */
88 if (eoln) *eoln = '\n';
89 }
90 return value;
91 }
92
get_int_data(char * msg,char * search)93 int get_int_data(char *msg,char *search)
94 {
95 char *p,*r,*eoln;
96 int value=0;
97
98 /* go to the start of the message */
99 p = strstr(msg, search);
100 if (p) {
101 /* set the endofline */
102 eoln = strchr(p, '\n'); if (eoln) *eoln = '\0';
103 /* search for a ":" or "=" */
104 if ((r = strchr(p,':')) == NULL) r=strchr(p,'=');
105 // dbgprintf("\n1getdata\n%s\n",p);
106 // dbgprintf("\n2getdata\n%s\n",r);
107 if (r) value=atoi(++r);
108 /* reset the endofline */
109 if (eoln) *eoln = '\n';
110 }
111 return value;
112 }
113
114
115 typedef struct sectlist_t {
116 char *sname;
117 char *sdata;
118 struct sectlist_t *next;
119 } sectlist_t;
120 sectlist_t *sections = NULL;
121
splitmsg(char * clientdata)122 void splitmsg(char *clientdata)
123 {
124 char *cursection, *nextsection;
125 char *sectname, *sectdata;
126
127 /* Free the old list */
128 if (sections) {
129 sectlist_t *swalk, *stmp;
130
131 swalk = sections;
132 while (swalk) {
133 stmp = swalk;
134 swalk = swalk->next;
135 xfree(stmp);
136 }
137
138 sections = NULL;
139 }
140
141 if (clientdata == NULL) {
142 errprintf("Got a NULL client data message\n");
143 return;
144 }
145
146 /* Find the start of the first section */
147 if (*clientdata == '[')
148 cursection = clientdata;
149 else {
150 cursection = strstr(clientdata, "\n[");
151 if (cursection) cursection++;
152 }
153
154 while (cursection) {
155 sectlist_t *newsect = (sectlist_t *)malloc(sizeof(sectlist_t));
156
157 /* Find end of this section (i.e. start of the next section, if any) */
158 nextsection = strstr(cursection, "\n[");
159 if (nextsection) {
160 *nextsection = '\0';
161 nextsection++;
162 }
163
164 /* Pick out the section name and data */
165 sectname = cursection+1;
166 sectdata = sectname + strcspn(sectname, "]\n");
167 *sectdata = '\0'; sectdata++; if (*sectdata == '\n') sectdata++;
168
169 /* Save the pointers in the list */
170 newsect->sname = sectname;
171 newsect->sdata = sectdata;
172 newsect->next = sections;
173 sections = newsect;
174
175 /* Next section, please */
176 cursection = nextsection;
177 }
178 }
179
getdata(char * sectionname)180 char *getdata(char *sectionname)
181 {
182 sectlist_t *swalk;
183
184 for (swalk = sections; (swalk && strcmp(swalk->sname, sectionname)); swalk = swalk->next) ;
185 if (swalk) return swalk->sdata;
186
187 return NULL;
188 }
189
190