Blame view

check_memfree/check_memfree.cpp 4.08 KB
Imanol-Mikel Barba Sabariego authored
1
2
3
4
5
6
#include "check_memfree.h"

using namespace std;

char *servicename = (char*)"MEMORY";
Imanol-Mikel Barba Sabariego authored
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
void getMem(ifstream *file, MEMINFO *meminfo)
{
	meminfo->freeMem = 0;
	meminfo->totalMem = 0;
	string line, field, value;
	getline(*file,line);
	do
	{
		field = line.substr(0,line.find_first_of(':'));
		value = line.substr(line.find_first_of(' '),line.find_last_of(' '));
		if(field == "MemTotal")
		{
			meminfo->totalMem = str2int(value);
		}
		else if(field == "MemFree")
		{
			meminfo->freeMem += str2int(value);
		}
		else if(field == "Buffers")
		{
			meminfo->freeMem += str2int(value);
		}
		else if(field == "Cached")
		{
			meminfo->freeMem += str2int(value);
		}
		getline(*file,line);
	}while(!file->eof());
}
Imanol-Mikel Barba Sabariego authored
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
int getNum(ifstream *file)
{
	string numStr = "";
	int num;
	file->ignore(numeric_limits<streamsize>::max(),' ');
	(*file) >> numStr;
	try
	{
		num = str2int(numStr);
	}
	catch(integerConversionException e)
	{
		cout << e.what() << endl;
		exit(3);
	}
	getline(*file,numStr);
	return num;
}

void printVersion()
{
	cout << "check_memfree v" << VERSION << endl << endl;
}

void printHelp(bool longVersion)
{
	if(longVersion)
	{
		printVersion();
		cout << "Check free memory space on local machine." << endl << endl;
		printHelp(false);
		cout << "Options:" << endl;
		cout << " -h" << endl;
		cout << "    Print detailed help screen" << endl;
		cout << " -V" << endl;
		cout << "    Print version information" << endl;
		cout << " -w INTEGER" << endl;
		cout << "    Exit with WARNING status if less than INTEGER bytes of memory space are free" << endl;
		cout << " -w PERCENT%" << endl;
		cout << "    Exit with WARNING status if less than PERCENT of memory space is free" << endl;
		cout << " -c INTEGER" << endl;
		cout << "    Exit with CRITICAL status if less than INTEGER bytes of memory space are free" << endl;
		cout << " -c PERCENT%" << endl;
		cout << "    Exit with CRITCAL status if less than PERCENT of memory space is free" << endl << endl;
		return;
	}
	cout << "Usage: " << endl << "check_memfree [-hV] -w <percent_free>% -c <percent_free>%" << endl << "check_memfree [-hV] -w <bytes_free> -c <bytes_free>" << endl << endl;
}

int main(int argc, char **argv)
{
	struct itimerval timer;
	timer.it_value.tv_sec = 10;
	timer.it_value.tv_usec = 0;
	timer.it_interval.tv_sec = 0;
	timer.it_interval.tv_usec = 0;
	setitimer (ITIMER_VIRTUAL, &timer, 0);

	struct sigaction sa;
	memset (&sa, 0, sizeof (sa));
	sa.sa_handler = &timer_handler;
	sigaction (SIGVTALRM, &sa, 0);

	char *warningFlag = NULL;
	char *criticalFlag = NULL;
	int c;

	while ((c = getopt (argc, argv, "w:c:Vh")) != -1)
	{
		switch(c)
		{
			case 'w':
        			warningFlag = optarg;
        			break;
			case 'c':
       		 		criticalFlag = optarg;
       		 		break;
			case 'V':
				printVersion();
				return 0;
			case 'h':
				printHelp(true);
				return 0;
			case '?':
				printHelp(false);
				return 3;
		}
	}

	if(warningFlag == NULL)
	{
		cout << "Warning limit (-w) wasn't set. Exiting." << endl;
		return 3;
	}
	if(criticalFlag == NULL)
	{
		cout << "Critical limit (-w) wasn't set. Exiting." << endl;
		return 3;
	}
Imanol-Mikel Barba Sabariego authored
137
138
	MEMINFO meminfo;
	ifstream meminfo_file(MEMINFO_FILE);
Imanol-Mikel Barba Sabariego authored
139
Imanol-Mikel Barba Sabariego authored
140
	getMem(&meminfo_file,&meminfo);
Imanol-Mikel Barba Sabariego authored
141
Imanol-Mikel Barba Sabariego authored
142
	meminfo_file.close();
Imanol-Mikel Barba Sabariego authored
143
Imanol-Mikel Barba Sabariego authored
144
145
	int freeMemMB = meminfo.freeMem / 1024;
	int totalMemMB = meminfo.totalMem / 1024;
Imanol-Mikel Barba Sabariego authored
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
	int percFree = ((double)freeMemMB/(double)totalMemMB)*100;

	range warningLimit, criticalLimit;

	try
	{
		warningLimit = parseRange(warningFlag,totalMemMB);
		criticalLimit = parseRange(criticalFlag,totalMemMB);
	}
	catch(rangeException e)
	{
		cout << e.what() << endl;
		exit(3);
	}

	int returnCode;

	cout << servicename;

	if((freeMemMB > criticalLimit.min) && (freeMemMB < criticalLimit.max))
	{
		cout << " CRITICAL";
		returnCode = 2;
	}
	else if((freeMemMB > warningLimit.min) && (freeMemMB < warningLimit.max))
	{
		cout << " WARNING";
		returnCode = 1;
	}
	else
	{
		cout << " OK";
		returnCode = 0;
	}
	cout << " - " << percFree << "% free (" << freeMemMB << " MB out of " << totalMemMB << " MB) |memory=" << freeMemMB << "MB;" << warningLimit.max << ";" << criticalLimit.max << ";" << "0;" << totalMemMB << endl;
	return returnCode;
}
Imanol-Mikel Barba Sabariego authored
183