-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpingtest.cpp
74 lines (70 loc) · 1.65 KB
/
pingtest.cpp
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
#include<iostream>
#include<fstream>
#include<regex>
#include<set>
#include<time.h>
#include<unistd.h>
using namespace std;
ifstream fin;
ofstream fout;
set<string> ips;
time_t rawtime;
struct tm* timeinfo;
string gettime(){
time(&rawtime);
timeinfo=localtime(&rawtime);
string tmp=string(asctime(timeinfo));
tmp.pop_back();
return tmp;
}
void logit(string str){
fout<<'['<<gettime()<<"] "<<str<<endl;
}
int main(){
cout<<"Starting Time: "<<gettime()<<endl;
fin.open("config.txt");
if(!fin.is_open()){
cout<<"No Config File!"<<endl;
return 0;
}
string str;
while(fin>>str){
if(regex_match(str,regex("((25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))")))ips.insert(str);
}
if(ips.empty()){
cout<<"No IP!"<<endl;
return 0;
}
cout<<"IP list: ";
for(const auto& ipaddr:ips)cout<<ipaddr<<' ';
cout<<"\nStarting Log!"<<endl;
fout.open(string(gettime()+".log").c_str());
logit("Monitor Started!");
bool status=1;
bool prepare=1;
while(1){
unsigned count=0;
for(const auto& ipaddr:ips){
str="fping "+ipaddr+" 1>/dev/null 2>&1";
//cout<<str<<endl;
if(!system(str.c_str()))count++;
else{
if(status)logit(ipaddr+string(" is not alive."));
}
}
if(prepare){
if(count==ips.size())logit("All hosts are alive!");
else if(!count)logit("All hosts are NOT alive!"),status=0;
else status=0;
prepare=0;
}else{
if(count==ips.size()){
if(!status)logit("All hosts are alive!"),status=1;
}else if(!count){
if(status)logit("All hosts are NOT alive!"),status=0;
}
}
sleep(60);
}
return 0;
}