-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_weights.c
315 lines (238 loc) · 8.42 KB
/
update_weights.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <pthread.h>
typedef struct {
int cpu;
int memory;
int connections;
} Coeficients;
typedef struct {
unsigned int cpu_free;
unsigned int mem_free;
unsigned int connections;
} Metrics;
typedef struct {
const char *name;
Metrics metrics;
unsigned int old_weight;
unsigned int weight;
int up;
} Host;
struct main_thread_info { /* Used as argument to run() */
int periodicity;
int num_servers;
};
struct server_thread_info { /* Used as argument to server_run() */
int id;
};
void *run(void *arg);
void *server_run(void *arg);
void determineNewWeights(int num_servers, Host *servers);
void getMetrics(Host *server);
FILE *exec_wget(const char *name_ip);
FILE *exec_snmp(char *type, int version, const char *community, const char *name_ip, char *OID);
FILE *exec_command(char *command);
static Host *servers;
static Coeficients *coefs;
int main(int argc, char **argv)
{
pthread_t thread;
int ret, i;
struct main_thread_info *tinfo;
tinfo = malloc(sizeof(struct main_thread_info));
tinfo->periodicity = 5;
tinfo->num_servers = 2;
coefs = (Coeficients *) malloc(sizeof(Coeficients));
coefs->cpu = 40;
coefs->memory = 40;
coefs->connections = 20;
servers = (Host *) malloc(tinfo->num_servers * sizeof(Host));
servers[0].name = "192.168.56.102";
servers[0].old_weight = 1;
servers[1].name = "192.168.56.103";
servers[1].old_weight = 1;
ret = pthread_create(&thread, NULL, run, (void*) tinfo);
pthread_join(thread, NULL);
printf("Thread returns: %d\n", ret);
free(tinfo);
free(coefs);
free(servers);
return (EXIT_SUCCESS);
}
void *run(void *arg) {
int i, ret, cnt = 1, new_weight;
pthread_t *server_threads;
struct main_thread_info *tinfo = (struct main_thread_info *) arg;
struct server_thread_info *sinfo;
int num_servers = tinfo->num_servers;
server_threads = malloc(num_servers * sizeof (pthread_t));
sinfo = (struct server_thread_info *) malloc(num_servers * sizeof(struct server_thread_info));
printf("Main thread started!\n");
while (1) {
printf("Data collection %d!\n", cnt++);
for (i = 0; i < num_servers; i++) {
sinfo[i].id = i;
ret = pthread_create(&server_threads[i], NULL, server_run, (void*) &(servers[i]));
}
for (i = 0; i < num_servers; i++) {
pthread_join(server_threads[i], NULL);
}
determineNewWeights(num_servers, servers);
sleep(tinfo->periodicity);
}
free(server_threads);
}
void *server_run(void *arg) {
Host *server = (Host *) arg;
int cpu_free, num_conn, mem_free;
int version = 1, id;
unsigned int weight = 0;
//id = sinfo->id;
printf("Thread Server %s started!\n", server->name);
if(http_port_open(server->name))
{
if(server->up == 0)
server->up = 1;
getMetrics(server);
printf("Free %% CPU for server %s = %d\n", server->name, server->metrics.cpu_free);
printf("Free %% Memory for server %s = %d\n", server->name, server->metrics.mem_free);
printf("Number of connections for server %s = %d\n", server->name, server->metrics.connections);
if (server->metrics.cpu_free == -1)
server->metrics.cpu_free = 0;
if (server->metrics.mem_free == -1)
server->metrics.mem_free = 0;
}
else
server->up = 0;
printf("Thread Server %s ended!\n", server->name);
}
void determineNewWeights(int num_servers, Host *servers) {
int i;
unsigned int min, max;
max = 0;
min = UINT_MAX;
for (i = 0; i < num_servers; i++) {
if((servers[i].metrics.connections > 0) && (servers[i].metrics.connections > max))
max = servers[i].metrics.connections;
}
for (i = 0; i < num_servers; i++) {
if (servers[i].metrics.connections == -1)
servers[i].metrics.connections = max;
if(max > 0)
servers[i].metrics.connections = (int) ((1 - (double) servers[i].metrics.connections/max) * 100 + 0.5);
printf("Server %s determined weight for connections = %d\n", servers[i].name, servers[i].metrics.connections);
servers[i].weight = (int) ((double) (coefs->cpu * servers[i].metrics.cpu_free + coefs->memory * servers[i].metrics.mem_free + coefs->connections * servers[i].metrics.connections) + 0.5);
printf("Server %s determined weight = %d\n", servers[i].name, servers[i].weight);
if((servers[i].weight > 0) && (servers[i].weight < min))
min = servers[i].weight;
}
for (i = 0; i < num_servers; i++) {
if(min > 0)
servers[i].weight = (int) ((double) servers[i].weight/min + 0.5);
if(servers[i].weight > 0)
{
servers[i].weight = servers[i].old_weight + servers[i].weight;
servers[i].weight = (int) ((double) servers[i].weight / 2 + 0.5);
}
else
servers[i].weight = servers[i].old_weight;
if(servers[i].up && servers[i].weight == 0)
servers[i].weight = 1;
servers[i].old_weight = servers[i].weight;
printf("Server %s new weight = %d\n", servers[i].name, servers[i].weight);
}
}
int http_port_open(const char *name_ip) {
FILE *fp;
char output[1024], *result;
int counter = 0;
fp = exec_wget(name_ip);
while (fgets(output, sizeof (output) - 1, fp) != NULL) {
counter++;
if(counter == 2 && strstr(output, "Connecting") != NULL && strstr(output, "connected") != NULL) {
fclose(fp);
return 1;
}
if(counter > 2)
break;
}
fclose(fp);
return 0;
}
void getMetrics(Host *server) {
FILE *fp;
char output[1024], OIDs[1024];
int counter = 0;
char *result = NULL, *res;
int cpu_free = -1, mem_free = -1, mem_total = -1, connections = -1;
/* Free CPU percentage */
fp = exec_snmp("walk", 1, "public", server->name, "1.3.6.1.2.1.25.3.3.1.2");
while (fgets(output, sizeof (output) - 1, fp) != NULL) {
if (strstr(output, "Timeout") == NULL) {
counter++;
result = (char *) strtok(output, " ");
while (result != NULL) {
res = result;
result = (char *) strtok(NULL, " ");
}
result = (char *) strtok(res, "\r");
if(counter == 1)
cpu_free = atoi(result);
else
cpu_free += atoi(result);
}
}
pclose(fp);
if (cpu_free > -1 && counter > 0) {
cpu_free = (int) ((double) cpu_free/counter + 0.5);
cpu_free = 100 - cpu_free;
}
server->metrics.cpu_free = cpu_free;
/* Free Memory percentage, established TCP connections */
fp = exec_snmp("get", 1, "public", server->name, "1.3.6.1.4.1.2021.4.11.0 1.3.6.1.4.1.2021.4.5.0 1.3.6.1.2.1.6.9.0");
while (fgets(output, sizeof (output) - 1, fp) != NULL) {
if (strstr(output, "Timeout") == NULL) {
result = (char *) strtok(output, " ");
while (result != NULL) {
res = result;
result = (char *) strtok(NULL, " ");
}
result = (char *) strtok(res, "\r");
if (strstr(output, "1.3.6.1.4.1.2021.4.11.0") != NULL)
mem_free = atoi(result);
else if (strstr(output, "1.3.6.1.4.1.2021.4.5.0") != NULL)
mem_total = atoi(result);
else if (strstr(output, "1.3.6.1.2.1.6.9") != NULL)
connections = atoi(result);
}
}
pclose(fp);
if(mem_free > -1 && mem_total > 0)
server->metrics.mem_free = (int) ((((double) mem_free/mem_total) * 100) + 0.5);
else
server->metrics.mem_free = -1;
server->metrics.connections = connections;
}
FILE *exec_wget(const char *name_ip) {
char command[1024];
sprintf(command, "wget -O - %s 2>&1", name_ip);
return exec_command(command);
}
FILE *exec_snmp(char *type, int version, const char *community, const char *name_ip, char *OID) {
char command[1024];
sprintf(command, "snmp%s -O enU -v %d -c %s %s %s", type, version, community, name_ip, OID);
return exec_command(command);
}
FILE *exec_command(char *command) {
FILE *fp;
/* Open the command for reading. */
fp = popen(command, "r");
if (fp == NULL) {
printf("Failed to run command\n");
exit;
}
return fp;
}