-
Notifications
You must be signed in to change notification settings - Fork 0
/
im_alive_client.c
571 lines (533 loc) · 20.1 KB
/
im_alive_client.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/types.h>
#ifdef __APPLE__
#include <sys/sysctl.h>
#include <mach/vm_statistics.h>
#include <mach/mach_types.h>
#include <mach/mach_init.h>
#include <mach/mach_host.h>
#else
#include <linux/ethtool.h>
#include <linux/sockios.h>
#include <sys/sysinfo.h>
#endif
#include <unistd.h>
#include <utmpx.h>
#include <netdb.h>
#include <signal.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include "im_alive.h"
#include "debug.h"
struct interface {
int flags; /* IFF_UP etc. */
long speed; /* Mbps; -1 is unknown */
int duplex; /* DUPLEX_FULL, DUPLEX_HALF, or unknown */
char name[IF_NAMESIZE + 1];
};
configuration myConfig;
static void sig_handler(int sig) {
switch (sig) {
case SIGTERM: myConfig.loop=0; break;
case SIGUSR1: myConfig.period= MAX(0,MIN(myConfig.period+30,300)); break;
case SIGUSR2: myConfig.period= MAX(0,MIN(myConfig.period-30,300)); break;
default: debug(DBG_ERROR,"unexpected signal '%d' received\n",sig); break;
}
}
char *getServer() {
char tmpbuf[16];
if (myConfig.binary_host) return myConfig.binary_host;
#ifdef __APPPLE__
myConfig.binary_host=strdup("-");
#else
// parse kernel command line to extract image name
char *cmdline=calloc(512,sizeof (char));
int f=open("/proc/cmdline",O_RDONLY);
if (!cmdline || f<0) return "-";
read(f,cmdline,511);
close(f);
char *pt=strstr(cmdline,"138.4.30.5");
if (pt) {
// image server is binarioX, evaluate X
snprintf(tmpbuf, 15, "binario%c", *(pt + 10));
myConfig.binary_host = strdup(tmpbuf);
} else {
// image server is not a binary: call gethostbyaddr to retrieve host name
pt=strstr(cmdline,"nbdroot=");
if (!pt) {
debug(DBG_ERROR,"NBD Server ip not found from kernel command line");
myConfig.binary_host=strdup("-");
} else {
// NOTA: asumimos que la linea está bien formada, pues de lo contrario la maquina no arrancaría
char *from=1+strchr(pt,'=');
char *strto=strchr(pt,':');
if(strto) *strto='\0';
// ahora buscamos el hostname asociado a esta ip
struct hostent *ent2= gethostbyaddr( from, strlen(from), AF_INET);
char *dot=strchr(ent2->h_name,'.');
if (dot) *dot='\0'; // remove FQDN part if any
myConfig.binary_host=strdup(ent2->h_name);
}
free(cmdline);
return myConfig.binary_host;
}
#endif
static char *result=NULL;
if (result) return result; // already evaluated
struct hostent *ent;
sethostent(0);
while ( (ent=gethostent()) ) {
int flag=0;
if (strstr(ent->h_name,"server")) flag=1;
if (strstr(ent->h_name,"binario")) flag=1;
if (strstr(ent->h_name,"maestro")) flag=1;
if (!flag) continue;
endhostent();
struct hostent *ent2= gethostbyaddr( ent->h_addr_list[0], sizeof(ent->h_addr_list[0]), AF_INET);
char *dot=strchr(ent2->h_name,'.');
if (dot) *dot='\0'; // remove FQDN part if any
result=calloc(1+strlen(ent2->h_name),sizeof(char));
strncpy(result,ent2->h_name,strlen(ent2->h_name));
return result;
}
endhostent();
result="-";
return result;
}
/*
* to register gdm sessions don't forget to include /etc/gdm/PreSession/Default
* sessreg -a -w /var/log/wtmp -u /var/run/utmp -x "$X_SERVERS" -h "$REMOTE_HOST" -l "$DISPLAY" "$USER"
*/
/* from https://stackoverflow.com/questions/31472040/program-to-display-all-logged-in-users-via-c-program-on-ubuntu */
char * getUsers() {
static char *buff=NULL;
struct utmpx *n;
time_t to=time(NULL);
char where[6];
if (!buff) buff=calloc(1024,sizeof(char));
if (!buff) return "-";
memset(buff,0,1024);
setutxent();
n=getutxent();
while(n) {
if(n->ut_type==USER_PROCESS) {
// remove extra parts from username
char *user=strdup(&n->ut_user[0]);
char *p=strchr(user,'@');
if (p) *p=0;
// insert into list if not already inserted
// if (!strstr(buff,&n->ut_user[0])) {
if (!strstr(buff,user)) {
// evaluate how many time active
time_t secs=to - n->ut_tv.tv_sec;
// look for access type (G)raphics,(C)console,(R)remote,(S)sh
if (strlen(n->ut_host)==0) snprintf(where,6,"(txt)"); // tty console (empty host)
else if (strstr(&n->ut_host[0],"127")) snprintf(where,6,"(rem)"); // XVnc (127.0.0.1)
else if (strstr(&n->ut_host[0],":")) snprintf(where,6,"(con)"); // Console (:2)
else snprintf(where,6,"(ssh)"); // remote ssh access (ptyX)
size_t len=strlen(buff);
snprintf(buff+len,1000-len,",%s %02ldh%02ldm %s",user,secs/3600,(secs/60)%60,where);
}
}
n=getutxent();
}
endutxent();
if (*buff=='\0') return "-";
// notice that browser will translate "," to "<br/> on returning text, so don't skip first comma
return buff;
}
// call to uptime. return number of seconds from client start
long getUptime() {
#ifdef __APPLE__
// from https://stackoverflow.com/questions/3269321/osx-programmatically-get-uptime
struct timeval boottime;
size_t len = sizeof(boottime);
int mib[2] = { CTL_KERN, KERN_BOOTTIME };
if( sysctl(mib, 2, &boottime, &len, NULL, 0) < 0 ) return -1;
time_t bsec = boottime.tv_sec; // running time
time_t csec = time(NULL); // current time
return (long) difftime(csec, bsec);
#else
double uptime_secs=1;
size_t num_bytes_read;
int fd;
char read_buf[128];
double idle_secs;
fd = open("/proc/uptime", O_RDONLY, S_IRUSR | S_IRGRP | S_IROTH);
if(fd <0) {
debug(DBG_ERROR,"cannot open /proc/uptime");
return -1;
}
num_bytes_read=read(fd, read_buf, 128);
if (num_bytes_read<0) {
debug(DBG_ERROR,"read() error in /proc/uptime");
close(fd);
return -1;
}
if(sscanf(read_buf, "%lf %lf", &(uptime_secs), &(idle_secs)) != 2) {
debug(DBG_ERROR,"Erron on sscanf /proc/uptime, %s, expected two floats", strerror(errno));
}
close(fd);
return (long) uptime_secs;
#endif
}
char *getComputerModel() {
static char buffer[64];
memset(buffer,0,sizeof(buffer));
#ifdef __APPLE__
size_t len = 0;
int mib[2]={CTL_HW,HW_MODEL};
// according to doc need first to eval length
sysctl(mib,2, NULL, &len, NULL, 0);
if ((len==0)||(len>63)) {
debug(DBG_ERROR,"cannot retrieve hardware model");
snprintf(buffer,63,"Mac-unknown");
} else {
// and then get data
sysctl(mib,2, buffer, &len, NULL, 0);
}
#else
FILE *fp=popen("dmidecode -s system-product-name", "r");
if (fp == NULL) {
debug(DBG_ERROR,"cannot retrieve (dmidecode) hardware model");
snprintf(buffer,63,"PC-unknown");
return buffer;
}
/* Read the output a line at a time - output it. */
if (! fgets(buffer, sizeof(buffer), fp)) {
debug(DBG_ERROR,"Dmidecode returns no Product Name info");
snprintf(buffer,63,"PC-unknown");
}
/* close */
pclose(fp);
// dmidecode returns data ending with '\n', so remove it
buffer[strlen(buffer)-1]='\0';
#endif
return buffer;
}
#ifdef __APPLE__
char *getIfStatus() {
debug(DBG_INFO,"Cannot evaluate network parameters in Mac-OSX");
return "-";
}
#else
static int get_interface_common(const int fd, struct ifreq *const ifr, struct interface *const info) {
struct ethtool_cmd cmd;
int result;
/* Interface flags. */
if (ioctl(fd, SIOCGIFFLAGS, ifr) == -1) info->flags = 0;
else info->flags = ifr->ifr_flags;
/* "Get interface settings" */
ifr->ifr_data = (void *)&cmd;
cmd.cmd = ETHTOOL_GSET;
if (ioctl(fd, SIOCETHTOOL, ifr) == -1) { // ioctl failed: set parameters to unknown
info->speed = -1L;
info->duplex = DUPLEX_UNKNOWN;
} else { // ioctl success: set parameters
info->speed = ethtool_cmd_speed(&cmd);
info->duplex = cmd.duplex;
}
// try to close socket
do { result = close(fd); } while (result == -1 && errno == EINTR);
if (result == -1) return errno;
return 0;
}
char *getIfStatus() {
static char buffer[48]; // where result is stored
struct interface iface;
struct ifreq ifr;
int result;
// creamos un socket para ir buscando interfaces
int socketfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (socketfd == -1) {
debug(DBG_ERROR,"Cannot create socket to iterate interfaces");
snprintf(buffer,sizeof(buffer),"-");
return buffer;
}
for (int index = 1; ; index++) { // buscamos el interface con indice index
ifr.ifr_ifindex = index;
if (ioctl(socketfd, SIOCGIFNAME, &ifr) == -1) { // search interface at index "index"
// arriving here means that there are no more interfaces
do { result = close(socketfd); } while (result == -1 && errno == EINTR);
debug(DBG_ERROR,"Cannot find more interfaces. index:%d",index);
snprintf(buffer,sizeof(buffer),"-");
return buffer;
}
strncpy(iface.name, ifr.ifr_name, IF_NAMESIZE);
iface.name[IF_NAMESIZE] = '\0';
// si no tiene la ip que buscamos, continuamos la busqueda
ioctl(socketfd, SIOCGIFADDR, &ifr);
if (!strstr(inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr),"138.4.3")) continue;
// obtenemos datos y cerramos socket
result=get_interface_common(socketfd, &ifr, &iface);
if (result!=0) {
debug(DBG_ERROR,"Cannot get interface data '%s'",iface.name);
snprintf(buffer,sizeof(buffer),"%s ? Mbps ? duplex",iface.name);
return buffer;
}
// ok: evaluamos velocidad, modo y retornamos valores
snprintf(buffer,sizeof(buffer),"%s%s %ld Mbps %s duplex",
iface.name,
(iface.flags & IFF_UP)? " up":"",
(iface.speed > 0)? iface.speed:0,
(iface.duplex == DUPLEX_FULL)? "full": ((iface.duplex == DUPLEX_HALF)?"half":"?")
);
debug(DBG_INFO,"Interface index: %d status %s",index,buffer);
return buffer;
}
// arriving here means error. should never happen
debug(DBG_ALERT,"Cannot locate any network interface !!!");
sprintf(buffer,"-");
return buffer;
}
#endif
char *getLoad() {
static char buffer[32];
double load[3];
memset(buffer,0,sizeof(buffer));
if (getloadavg(load,3)<0) {
debug(DBG_ERROR,"cannot get loadavg");
snprintf(buffer,32,"0.0 / 0.0 / 0.0");
} else {
snprintf(buffer,32,"%.2f / %.2f / %.2f",load[0],load[1],load[2]);
}
return buffer;
}
#ifdef __APPLE__
// from https://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process
char *getMemInfo() {
static char buffer[32];
// get physical memory
int mib[2];
int64_t physical_memory;
size_t lenght;
mib[0]=CTL_HW;
mib[1]=HW_MEMSIZE;
lenght= sizeof(int64_t);
sysctl(mib,2, &physical_memory,&lenght,NULL,0);
// get used memory
vm_size_t page_size;
mach_port_t mach_port;
mach_msg_type_number_t count;
vm_statistics64_data_t vm_stats;
// long long free_memory=0;
long long used_memory=0;
mach_port = mach_host_self();
count = sizeof(vm_stats) / sizeof(natural_t);
if (KERN_SUCCESS == host_page_size(mach_port, &page_size) &&
KERN_SUCCESS == host_statistics64(mach_port, HOST_VM_INFO, (host_info64_t)&vm_stats, &count)) {
// free_memory = (int64_t)vm_stats.free_count * (int64_t)page_size;
used_memory = ((int64_t)vm_stats.active_count +
(int64_t)vm_stats.inactive_count +
(int64_t)vm_stats.wire_count) * (int64_t)page_size;
}
// return data in kilobytes
snprintf(buffer,32,"%lld / %lld", used_memory/1024,physical_memory/1024);
return buffer;
}
#else
// Linux: used memory evaluated as https://github.com/brndnmtthws/conky/issues/130
char *getMemInfo() {
static char buffer[32];
struct sysinfo s;
int res=sysinfo(&s);
if (res<0) {
debug(DBG_ERROR,"cannot get memory information");
snprintf(buffer,32,"0 / 0");
} else {
debug(DBG_DEBUG,"totalram:%ld used:%ld buffers:%ld",s.totalram,s.freeram,s.bufferram);
// return data in kilobytes.
snprintf(buffer,32,"%ld / %ld",
( (s.totalram - s.freeram - s.bufferram ) * s.mem_unit ) / 1024,
( s.totalram * s.mem_unit ) / 1024 );
}
return buffer;
}
#endif
char * getHostName() {
if (myConfig.client_host) return myConfig.client_host; // already evaluated
// extract client host name
char hostname[100];
memset(hostname,0,sizeof(hostname));
gethostname(hostname,100);
char *dot=strchr(hostname,'.');
if (dot) *dot='\0'; // remove FQDN part if any on hostname
myConfig.client_host=calloc(1+strlen(hostname),sizeof(char));
strncpy(myConfig.client_host,hostname,strlen(hostname));
return myConfig.client_host;
}
char * getImageName() {
if (myConfig.image_name) return myConfig.image_name;
#ifdef __APPPLE__
myConfig.imagename=strdup("MacOSX");
#else
// parse kernel command line to extract image name
char *cmdline=calloc(512,sizeof (char));
int f=open("/proc/cmdline",O_RDONLY);
if (!cmdline || f<0) return "-";
read(f,cmdline,511);
close(f);
if (strstr(cmdline,"FTEL")) myConfig.image_name=strdup("FTEL");
else if (strstr(cmdline,"LABDit")) myConfig.image_name=strdup("LABDit");
else if (strstr(cmdline,"EscritorioUPM")) myConfig.image_name=strdup("Escritorio UPM");
else if (strstr(cmdline,"thinstation")) myConfig.image_name=strdup("Remote Desktop");
else if (strstr(cmdline,"OLD")) myConfig.image_name=strdup("Ubuntu-18.04");
else myConfig.image_name=strdup("-");
#endif
return myConfig.image_name;
}
static int usage(char *progname) {
fprintf(stderr,"%s command line options:\n",progname);
fprintf(stderr,"\t -c client Client hostname [auto]\n");
fprintf(stderr,"\t -h host Server hostname [%s]\n",SERVER_HOST);
fprintf(stderr,"\t -p port UDP Server port [%d]\n",SERVER_UDPPORT);
fprintf(stderr,"\t -w port WSS Server port [%d]\n",SERVER_WSSPORT);
fprintf(stderr,"\t -l log_level Set debug/logging level 0:none thru 8:all. [3:error]\n");
fprintf(stderr,"\t -f log_file Set log file [%s]\n",LOG_FILE);
fprintf(stderr,"\t -t period Set loop period [%d] (secs)\n",DELAY_LOOP);
fprintf(stderr,"\t -d Run in backgroud (daemon). Implies no verbose [0]\n");
fprintf(stderr,"\t -v Send debug to stderr (verbose). Implies no daemon [0]\n");
fprintf(stderr,"\t -? Show this help\n");
return 0;
}
static int parse_cmdline(configuration *config,int argc, char *argv[]) {
int option;
while ((option = getopt(argc, argv,"c:h:w:p:l:f:t:dv")) != -1) {
switch (option) {
case 'c' : config->client_host = strdup(optarg); break;
case 'h' : config->server_host = strdup(optarg); break;
case 'p' : config->server_udpport = (int)strtol(optarg,NULL,10);break;
case 'w' : config->server_wssport = (int)strtol(optarg,NULL,10);break;
case 'l' : config->log_level = (int)strtol(optarg,NULL,10)%9; break;
case 't' : config->period = MAX(0,MIN((int)strtol(optarg,NULL,10),300)); break;
case 'f' : config->log_file = strdup(optarg); break;
case 'v' : config->verbose = 1; config->daemon = 0; break;
case 'd' : config->verbose = 0; config->daemon = 1; break;
case '?' : usage(argv[0]); exit(0);
default: return -1;
}
}
return 0;
}
// im_alive_client [-v] [-d] [ -h host ] [ -p port ] [-l level]
int main(int argc, char *argv[]) {
struct sockaddr_in server_address;
// default configuration
myConfig.image_name=NULL;
myConfig.binary_host=NULL;
myConfig.server_host=strdup(SERVER_HOST);
myConfig.server_udpport=SERVER_UDPPORT;
myConfig.server_wssport=SERVER_WSSPORT;
myConfig.log_file=strdup(LOG_FILE);
myConfig.log_level=3;
myConfig.verbose=0; // also send logging to stderr 0:no 1:yes
myConfig.daemon=0; // run in background
myConfig.loop=1;
myConfig.period=DELAY_LOOP; // 1 minute loop on client
myConfig.expire=EXPIRE_TIME; // 90 seconds loop on server
if ( parse_cmdline(&myConfig,argc,argv)<0) {
fprintf(stderr,"error parsing cmd line options");
usage(argv[0]);
return 1;
}
debug_init(&myConfig);
// set server_address data
struct hostent *ent=gethostbyname(myConfig.server_host);
if (!ent) {
debug(DBG_ERROR,"gethostbyname");
return -1;
}
memset(&server_address, 0, sizeof(server_address));
memcpy((void *)&server_address.sin_addr, ent->h_addr_list[0], ent->h_length);
server_address.sin_family = AF_INET;
server_address.sin_port = htons(myConfig.server_udpport);
// open socket
int sock = socket(PF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
debug(DBG_ERROR,"socket");
return 1;
}
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 500000; /* wait for response 0.5 seg. otherwise ignore */
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0) {
debug(DBG_ERROR,"setsockopt");
return 1;
}
// retrieve computer model
char computermodel[100];
memset(computermodel,0,sizeof(computermodel));
snprintf(computermodel,sizeof(computermodel),"%s",getComputerModel());
char* data_to_send = calloc(BUFFER_LENGTH,sizeof(char));
char* response = calloc(BUFFER_LENGTH,sizeof(char));
if (!data_to_send) { debug(DBG_ERROR,"calloc(data_to_send"); return 1; }
if (!response) { debug(DBG_ERROR,"calloc(response)"); return 1; }
// take care on term signal to end loop
signal(SIGTERM, sig_handler);
// become daemon if requested
if (myConfig.daemon) {
pid_t pid=fork();
switch (pid) {
case -1: // error
debug(DBG_ERROR,"fork");
return 1;
case 0: // child
fclose(stdin);
fclose(stdout);
fclose(stderr);
setsid();
break;
default: // parent
debug(DBG_INFO,"fork pid:%d",pid);
return 0;
}
}
// create pid file
pid_t pid=getpid();
FILE *fp=fopen("/var/run/ImAlive_client.pid","w");
if (!fp) debug(DBG_ALERT,"Cannot create pid file %d",pid);
else { fprintf(fp,"%d",pid); fclose(fp); }
// enter loop. exit on kill or SIGTERM
while (myConfig.loop) {
// compose string to be sent
// snprintf(data_to_send,BUFFER_LENGTH-1,"%s:%ld:%s:%s",hostname,getUptime(),binario,getUsers());
snprintf(data_to_send,BUFFER_LENGTH-1,"%s:%ld:%s:%s:%s:%s:%s:%s:%s",
getHostName(),
getUptime(),
getServer(),
getUsers(),
getLoad(),
getMemInfo(),
computermodel,
getIfStatus(),
getImageName()
);
// send data
debug(DBG_INFO,"sent: '%s'\n", data_to_send);
sendto(sock, data_to_send, strlen(data_to_send), 0,(struct sockaddr*)&server_address, sizeof(server_address));
// received echoed data back
size_t len=recvfrom(sock, response, BUFFER_LENGTH, 0, NULL, NULL); // timeout at 0.5 segs
if (len<0) {
debug(DBG_ERROR,"recvfrom",strerror(errno));
} else {
response[len] = '\0';
debug(DBG_INFO,"received: '%s'\n", response);
}
sleep(DELAY_LOOP);
}
// close the socket
close(sock);
// remove pid file and exit
unlink("/var/run/ImAlive_client.pid");
return 0;
}