Blame view

check_memfree_freebsd/auxiliar.cpp 1000 Bytes
Imanol-Mikel Barba Sabariego authored
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
//
// 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;
}

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;
}