-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
186 lines (134 loc) · 3.3 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include "get_usage.h"
#define IF_NAME "bond0"
#define DST_IP "40.10.5.1"
#define SERVER_PORT 6666
#define BUFFER_LEN 256
char *trimwhitespace(char *str)
{
char *end;
// Trim leading space
while(isspace(*str)) str++;
if(*str == 0)
return str;
end = str + strlen(str) - 1;
while(end > str && isspace(*end)) end--;
// Write new null terminator
*(end+1) = 0;
return str;
}
void err_exit(const char *err)
{
printf("%s\n", err);
exit(1);
}
int get_ip(char *ip)
{
int fd;
struct ifreq ifr;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd == -1)
{
//perror("socket create failed");
return -1;
}
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, IF_NAME, IFNAMSIZ-1);
ioctl(fd, SIOCGIFADDR, &ifr);
close(fd);
strcpy(ip, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
return 0;
}
int get_mac(const char *iface, char *mac)
{
FILE *fp;
char filename[512] = {0};
sprintf(filename, "/sys/class/net/%s/address", iface);
fp = fopen(filename, "r");
if(fp == NULL)
{
//printf("open file failed.\n");
return -1;
}
fscanf(fp, "%s", mac);
fclose(fp);
return 0;
}
int get_agent_status(const char *service)
{
char cmd[200] = {0};
char line[256] = {0};
snprintf(cmd, 200, "service %s status", service);
FILE *fp;
fp = popen(cmd, "r");
if(fp == NULL)
{
//printf("popen failed.\n");
return -1;
}
while(fgets(line, 256, fp) != NULL)
{
break;
}
pclose(fp);
if(strstr(line, "running"))
{
return 1;
}
//default: 0 stop
return 0;
}
int main(int argc, char **argv)
{
int sockfd;
//int n;
char sendline[BUFFER_LEN] = {0};
char ip_addr[16] = {0};
char mac_addr[18] = {0};
char ihostname[256] = {0};
long double cpu_usage = 0;
struct sockaddr_in servaddr;
#if 0
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
err_exit("create socket error");
}
bzero(&servaddr,sizeof servaddr);
servaddr.sin_family = AF_INET;
servaddr.sin_port=htons(SERVER_PORT);
inet_pton(AF_INET, DST_IP, &(servaddr.sin_addr));
int ret = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
if(ret == -1)
{
//perror("connect()");
return -1;
}
#endif
char sendline_dup[1000] = {0};
char cpu_mem[512] = {0};
char *servicename= "ssh_agent";
get_ip(ip_addr);
get_mac(IF_NAME, mac_addr);
gethostname(ihostname, sizeof(ihostname));
cpu_usage = get_system_cpu_usage();
snprintf(sendline_dup, sizeof(sendline_dup), "19 %s %s %s %Lf %f %f", ip_addr, mac_addr, ihostname,
cpu_usage, get_mem_info(), get_disk_usage("/"));
printf("the msg we send: %s.\n", sendline_dup);
#if 0
int n = write(sockfd, sendline_dup, strlen(sendline_dup));
printf("we have written %d bytes data.\n", n);
close(sockfd);
#endif
return 0;
}