-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASELSAN-Sysrepo-OS-Metrics.c
401 lines (341 loc) · 11.1 KB
/
ASELSAN-Sysrepo-OS-Metrics.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <sysrepo.h>
#include <libyang/libyang.h>
sr_session_ctx_t *sess;
sr_subscription_ctx_t *subscription;
char *get_date_output()
{
FILE *fp;
char buffer[128]; // Temporary buffer to hold the command output
size_t output_size = 128; // Initial buffer size for the final output
size_t used_size = 0; // Keeps track of the used size in the final buffer
// Allocate memory for the final output buffer
char *output = (char *)malloc(output_size);
if (output == NULL)
{
perror("malloc");
return NULL;
}
// Open the command for reading
fp = popen("date", "r");
if (fp == NULL)
{
perror("popen");
free(output);
return NULL;
}
// Read the output of the command
while (fgets(buffer, sizeof(buffer), fp) != NULL)
{
size_t len = strlen(buffer);
// Check if the final buffer needs to be resized
if (used_size + len + 1 > output_size)
{
output_size *= 2;
char *temp = (char *)realloc(output, output_size);
if (temp == NULL)
{
perror("realloc");
free(output);
pclose(fp);
return NULL;
}
output = temp;
}
// Append the buffer content to the final output
strcpy(output + used_size, buffer);
used_size += len;
}
// Close the pipe
pclose(fp);
return output;
}
int set_time(const char *time_str)
{
char cmd[sizeof("timedatectl set-ntp no; timedatectl set-time ") + 8];
snprintf(cmd, sizeof(cmd), "timedatectl set-ntp no; timedatectl set-time %s", time_str);
int result = system(cmd);
return result;
}
static int set_time_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *path, const sr_val_t *input,
const size_t input_cnt, sr_event_t event, uint32_t request_id, sr_val_t **output, size_t *output_cnt,
void *private_data)
{
(void)sub_id;
(void)path;
(void)input_cnt;
(void)event;
(void)request_id;
(void)private_data;
int res = set_time(input[0].data.string_val);
return res;
}
static int sync_time_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *path, const sr_val_t *input,
const size_t input_cnt, sr_event_t event, uint32_t request_id, sr_val_t **output, size_t *output_cnt,
void *private_data)
{
(void)input;
(void)sub_id;
(void)path;
(void)input_cnt;
(void)event;
(void)request_id;
(void)private_data;
int result = system("timedatectl set-ntp yes");
if (result != 0)
{
return SR_ERR_INTERNAL;
}
return SR_ERR_OK;
}
static int get_time_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *path, const sr_val_t *input,
const size_t input_cnt, sr_event_t event, uint32_t request_id, sr_val_t **output, size_t *output_cnt,
void *private_data)
{
sr_val_t *output_val;
(void)sub_id;
(void)path;
(void)input_cnt;
(void)event;
(void)request_id;
(void)private_data;
char *datetime = get_date_output();
if (datetime == NULL)
{
return SR_ERR_INTERNAL;
}
*output_cnt = 1;
*output = calloc(1, sizeof(**output));
output_val = *output;
output_val->xpath = strdup("/ASELSAN-Sysrepo-OS-Metrics:get-time/time");
output_val->type = SR_STRING_T;
output_val->data.string_val = datetime;
return SR_ERR_OK;
}
char *get_command_output(const char *command)
{
FILE *fp;
char buffer[1024]; // Temporary buffer to hold the command output
size_t output_size = 1024; // Initial buffer size for the final output
size_t used_size = 0; // Keeps track of the used size in the final buffer
// Allocate memory for the final output buffer
char *output = (char *)malloc(output_size);
if (output == NULL)
{
perror("malloc");
return NULL;
}
// Open the command for reading
fp = popen(command, "r");
if (fp == NULL)
{
perror("popen");
free(output);
return NULL;
}
// Read the output of the command
while (fgets(buffer, sizeof(buffer), fp) != NULL)
{
size_t len = strlen(buffer);
// Check if the final buffer needs to be resized
if (used_size + len + 1 > output_size)
{
output_size = used_size + len + 1; // Ensure enough space for new data and null terminator
char *temp = (char *)realloc(output, output_size);
if (temp == NULL)
{
perror("realloc");
free(output);
pclose(fp);
return NULL;
}
output = temp;
}
// Append the buffer content to the final output
memcpy(output + used_size, buffer, len);
used_size += len;
}
// Null-terminate the final output buffer
output[used_size] = '\0';
// Close the pipe
if (pclose(fp) == -1)
{
perror("pclose");
free(output);
return NULL;
}
return output;
}
static int get_uptime_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *path, const sr_val_t *input,
const size_t input_cnt, sr_event_t event, uint32_t request_id, sr_val_t **output, size_t *output_cnt,
void *private_data)
{
sr_val_t *output_val;
(void)sub_id;
(void)path;
(void)input_cnt;
(void)event;
(void)request_id;
(void)private_data;
char *out = get_command_output("uptime");
if (out == NULL)
{
return SR_ERR_INTERNAL;
}
*output_cnt = 1;
*output = calloc(1, sizeof(**output));
output_val = *output;
output_val->xpath = strdup("/ASELSAN-Sysrepo-OS-Metrics:uptime/out");
output_val->type = SR_STRING_T;
output_val->data.string_val = out;
return SR_ERR_OK;
}
static int get_freeg_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *path, const sr_val_t *input,
const size_t input_cnt, sr_event_t event, uint32_t request_id, sr_val_t **output, size_t *output_cnt,
void *private_data)
{
sr_val_t *output_val;
(void)sub_id;
(void)path;
(void)input_cnt;
(void)event;
(void)request_id;
(void)private_data;
char *out = get_command_output("free -g");
if (out == NULL)
{
return SR_ERR_INTERNAL;
}
*output_cnt = 1;
*output = calloc(1, sizeof(**output));
output_val = *output;
output_val->xpath = strdup("/ASELSAN-Sysrepo-OS-Metrics:freeg/out");
output_val->type = SR_STRING_T;
output_val->data.string_val = out;
return SR_ERR_OK;
}
static int get_lscpu_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *path, const sr_val_t *input,
const size_t input_cnt, sr_event_t event, uint32_t request_id, sr_val_t **output, size_t *output_cnt,
void *private_data)
{
sr_val_t *output_val;
(void)sub_id;
(void)path;
(void)input_cnt;
(void)event;
(void)request_id;
(void)private_data;
char *out = get_command_output("lscpu");
if (out == NULL)
{
return SR_ERR_INTERNAL;
}
*output_cnt = 1;
*output = calloc(1, sizeof(**output));
output_val = *output;
output_val->xpath = strdup("/ASELSAN-Sysrepo-OS-Metrics:lscpu/out");
output_val->type = SR_STRING_T;
output_val->data.string_val = out;
return SR_ERR_OK;
}
static int get_top_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *path, const sr_val_t *input,
const size_t input_cnt, sr_event_t event, uint32_t request_id, sr_val_t **output, size_t *output_cnt,
void *private_data)
{
sr_val_t *output_val;
(void)sub_id;
(void)path;
(void)input_cnt;
(void)event;
(void)request_id;
(void)private_data;
char *out = get_command_output("top -b -n 1");
if (out == NULL)
{
return SR_ERR_INTERNAL;
}
*output_cnt = 1;
*output = calloc(1, sizeof(**output));
output_val = *output;
output_val->xpath = strdup("/ASELSAN-Sysrepo-OS-Metrics:top/out");
output_val->type = SR_STRING_T;
output_val->data.string_val = out;
return SR_ERR_OK;
}
char *get_server_ip() {
char *cmd = "ifconfig | grep -Eo 'inet (addr:)?([0-9]*\\.){3}[0-9]*' | grep -Eo '([0-9]*\\.){3}[0-9]*' | grep -v '127.0.0.1'";
char *ip = malloc(INET_ADDRSTRLEN);
FILE *fp;
fp = popen(cmd, "r");
if (fp == NULL) {
perror("popen");
return NULL;
}
if (fgets(ip, INET_ADDRSTRLEN, fp) == NULL) {
perror("fgets");
pclose(fp);
return NULL;
}
// Remove newline character if present
ip[strcspn(ip, "\n")] = 0;
pclose(fp);
return ip;
}
/* Callback for the get-ip RPC */
static int
get_ip_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *path, const sr_val_t *input,
const size_t input_cnt, sr_event_t event, uint32_t request_id, sr_val_t **output, size_t *output_cnt,
void *private_data)
{
char *ip;
sr_val_t *output_val;
(void)sub_id;
(void)path;
(void)input;
(void)input_cnt;
(void)event;
(void)request_id;
(void)private_data;
ip = get_server_ip();
if (!ip) {
return SR_ERR_INTERNAL;
}
*output_cnt = 1;
*output = calloc(1, sizeof(**output));
output_val = *output;
output_val->xpath = strdup("/ASELSAN-Sysrepo-OS-Metrics:get-ip/ip-address");
output_val->type = SR_STRING_T;
output_val->data.string_val = ip;
return SR_ERR_OK;
}
int sr_plugin_init_cb(sr_session_ctx_t *session, void **private_data)
{
int rc;
sess = session;
/* Subscribe to get-ip RPC */
rc = sr_rpc_subscribe(session, "/ASELSAN-Sysrepo-OS-Metrics:freeg", get_freeg_cb, NULL, 0, 0, &subscription);
rc = sr_rpc_subscribe(session, "/ASELSAN-Sysrepo-OS-Metrics:uptime", get_uptime_cb, NULL, 0, 0, &subscription);
rc = sr_rpc_subscribe(session, "/ASELSAN-Sysrepo-OS-Metrics:lscpu", get_lscpu_cb, NULL, 0, 0, &subscription);
rc = sr_rpc_subscribe(session, "/ASELSAN-Sysrepo-OS-Metrics:top", get_top_cb, NULL, 0, 0, &subscription);
rc = sr_rpc_subscribe(session, "/ASELSAN-Sysrepo-OS-Metrics:set-time", set_time_cb, NULL, 0, 0, &subscription);
rc = sr_rpc_subscribe(session, "/ASELSAN-Sysrepo-OS-Metrics:get-time", get_time_cb, NULL, 0, 0, &subscription);
rc = sr_rpc_subscribe(session, "/ASELSAN-Sysrepo-OS-Metrics:sync-time", sync_time_cb, NULL, 0, 0, &subscription);
rc = sr_rpc_subscribe(session, "/ASELSAN-Sysrepo-OS-Metrics:get-ip", get_ip_cb, NULL, 0, 0, &subscription);
if (rc != SR_ERR_OK)
{
SRPLG_LOG_ERR("server", "Server plugin initialization failed: %s.", sr_strerror(rc));
return rc;
}
SRPLG_LOG_DBG("server", "Server plugin initialized successfully.");
return SR_ERR_OK;
}
void sr_plugin_cleanup_cb(sr_session_ctx_t *session, void *private_data)
{
(void)session;
(void)private_data;
sr_unsubscribe(subscription);
SRPLG_LOG_DBG("server", "Server plugin cleanup finished.");
}