-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcplug.cpp
More file actions
195 lines (169 loc) · 4.95 KB
/
cplug.cpp
File metadata and controls
195 lines (169 loc) · 4.95 KB
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
unsigned long correction2=0;
class cplug
{
int sock;
int port;
int connectRes;
string ipAddress;
int bytesReceived;
char buf[4608];
string strRec;
sockaddr_in hint;
double ram_avg=0,perusage;
double idle_avg=0,cpu_idle;
double io_avg=0,io;
char Main[500];
long correctionR=0,correctionIOC=0,correctionS=0;
public:
cplug() //Sorted
{
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock==-1)
cerr<<"\033[1;31mCan't create socket\033[0m";
}
void print_json()
{
cout<<Main<<endl;
}
void init_hint_struct() //Sorted
{
port=54000;
ipAddress="127.0.0.1";
hint.sin_family=AF_INET;
hint.sin_port=htons(port);
inet_pton(AF_INET,ipAddress.c_str(),&hint.sin_addr);
}
int connect_to_server()
{
connectRes=connect(sock,(sockaddr*)&hint,sizeof(hint));
if(connectRes==-1)
{
cerr<<"\033[1;31mConnection failed\033[0m";
return -1;
}
}
void complete_json()
{
strcat(Main," }");
}
void avg_each()
{
ram_avg+=perusage/itr;
idle_avg+=(100-cpu_idle)/itr;
io_avg+=io/itr;
}
void get_ram()
{
auto start=chrono::system_clock::now();
this_thread::sleep_for(chrono::microseconds(50000-correctionR-correction2));
ifstream ram;
string line;
ram.open("/proc/meminfo");
double ava,free,total,totFree,used;
while(ram>>line)
{
if(line=="MemTotal:")
{
ram>>total;
}
else if(line=="MemFree:")
{
ram>>free;
}
else if(line=="MemAvailable:")
ram>>ava;
}
ram.close();
totFree=ava;
used=total-totFree;
perusage=(used/total)*100;
// cout<<"\033[1;32m % Usage: \033[0m "<<setprecision(4)<<perusage<<" %"<<endl;
if(::count%(itr-1)==0)
{
// cout<<"Average: "<<ram_avg<<" %"<<endl;
strcpy(Main,"{ ");
maker(Main,GET_VARIABLE_NAME(ram_avg),ram_avg);
ram_avg=0;
}
auto endr=chrono::system_clock::now();
chrono::duration<double> elapsed_time=endr-start;
elapsed_time*=1000000;
correctionR=abs(elapsed_time.count()-50000);
// cout<<"\033[1;32m"<<correctionR/1000<<"\033[0m"<<endl;
}
int data_to_server()
{
complete_json();
print_json();
auto start=chrono::system_clock::now();
// this_thread::sleep_for(chrono::microseconds(10000000-correctionS));
int sendRes=send(sock,(const char*)&Main,sizeof(Main),0);
if(sendRes==-1)
{
cerr<<"\033[1;31m Can't Send! \033[0m"<<endl;
return -1;
}
auto ends=chrono::system_clock::now();
chrono::duration<double> elapsed_time=ends-start;
elapsed_time*=1000000;
correctionS=(elapsed_time.count()-10000000);
return 1;
}
void runAsync()
{
future<int> send4;
future<void> ram1=async(launch::async,&cplug::get_ram,this);
future<void> ioc2=async(launch::async,&cplug::get_cpu_idle_io,this);
future<void> avg3=async(launch::async,&cplug::avg_each,this);
if(::count%200==0)
send4=async(launch::async,&cplug::data_to_server,this);
ram1.get();
ioc2.get();
avg3.get();
if(::count%200==0)
send4.get();
}
void get_cpu_idle_io()
{
auto beg=chrono::system_clock::now();
this_thread::sleep_for(chrono::microseconds(50000-correctionIOC-correction2));
system("iostat > log_iostat.txt");
ifstream fin;
fin.open("log_iostat.txt");
string line;
double dump;
while(fin>>line)
{
if(line=="%idle")
{
fin>>dump;
fin>>dump;
fin>>dump;
fin>>io;
fin>>dump;
fin>>cpu_idle;
break;
}
}
// cout<<"\033[1;33m"<<(100-cpu_idle)<<" %"<<"\033[0m"<<endl;
// cout<<"\033[1;33m"<<io<<" %"<<"\033[0m"<<endl;
if(::count%(itr-1)==0)
{
// cout<<"\033[1;32m AVG: \033[0m"<<setprecision(4)<<idle_avg<<" %"<<endl;
maker(Main,GET_VARIABLE_NAME(idle_avg),idle_avg);
// cout<<"\033[1;32m AVG: \033[0m"<<setprecision(4)<<io_avg<<" %"<<endl;
maker(Main,GET_VARIABLE_NAME(io_avg),io_avg,stop);
idle_avg=0;
io_avg=0;
}
fin.close();
auto endr=chrono::system_clock::now();
chrono::duration<double> elapsed_time=endr-beg;
elapsed_time*=1000000;
correctionIOC=abs((elapsed_time.count()-50000));
}
~cplug()
{
close(sock);
}
};