auxiliar.cpp
1.19 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
//
// Created by Imanol on 28-may-16.
//
#include "auxiliar.h"
void timer_handler (int signum)
{
if(signum == SIGVTALRM)
{
cout << servicename << " CRITICAL - timeout occurred" << endl;
exit(2);
}
}
int str2int(string str)
{
int num;
stringstream sstream;
sstream << str;
if(!(sstream >> num))
{
throw integerConversionException("Integer conversion error");
}
return num;
}
string int2str(int x)
{
string str;
stringstream sstream;
sstream << x;
sstream >> str;
return str;
}
void split(const string &s, const char* delim, vector<string> & v)
{
char *dup = strdup(s.c_str());
char *token = strtok(dup, delim);
while(token != NULL)
{
v.push_back(string(token));
token = strtok(NULL, delim);
}
free(dup);
}
int exec(string cmd, string *output)
{
*output = "";
FILE* pipe = popen(cmd.c_str(), "r");
if (!pipe)
{
cout << "Error opening child process" << endl;
exit(3);
}
char buffer[128];
while(!feof(pipe))
{
if(fgets(buffer, 128, pipe) != NULL)
{
*output += buffer;
}
}
return pclose(pipe)/256;
}