-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_mem.c
127 lines (102 loc) · 2.61 KB
/
check_mem.c
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
#include <ctype.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define INVAL_SIZE UINT64_C(0xFFFFFFFFFFFFFFFF)
struct mem_stats
{
uint64_t total_kB;
uint64_t avail_kB;
uint64_t free_kB;
uint64_t buffers_kB;
uint64_t cached_kB;
uint64_t used_kB;
};
static void dump_mem_stats(struct mem_stats *mem_stats)
{
FILE *fp;
char line[1024];
unsigned int i;
char *nptr;
fp = fopen("/proc/meminfo", "r");
mem_stats->total_kB = INVAL_SIZE;
mem_stats->avail_kB = INVAL_SIZE;
mem_stats->free_kB = INVAL_SIZE;
mem_stats->buffers_kB = INVAL_SIZE;
mem_stats->cached_kB = INVAL_SIZE;
mem_stats->used_kB = INVAL_SIZE;
while (!feof(fp) && !ferror(fp))
{
if (!fgets(line, sizeof(line), fp))
continue;
if (strncmp(&line[0], "MemTotal:", 9) == 0)
{
mem_stats->total_kB = strtoull(&line[9], &nptr, 10);
continue;
}
if (strncmp(&line[0], "MemAvailable:", 13) == 0)
{
mem_stats->avail_kB = strtoull(&line[13], &nptr, 10);
continue;
}
if (strncmp(&line[0], "memFree:", 8) == 0)
{
mem_stats->free_kB = strtoull(&line[8], &nptr, 10);
continue;
}
if (strncmp(&line[0], "Buffers:", 8) == 0)
{
mem_stats->buffers_kB = strtoull(&line[8], &nptr, 10);
continue;
}
if (strncmp(&line[0], "Cached:", 7) == 0)
{
mem_stats->cached_kB = strtoull(&line[7], &nptr, 10);
continue;
}
}
fclose(fp);
if (mem_stats->avail_kB == INVAL_SIZE)
{
mem_stats->avail_kB = 0;
if (mem_stats->free_kB != INVAL_SIZE)
mem_stats->avail_kB += mem_stats->free_kB;
if (mem_stats->buffers_kB != INVAL_SIZE)
mem_stats->avail_kB += mem_stats->buffers_kB;
if (mem_stats->cached_kB != INVAL_SIZE)
mem_stats->avail_kB += mem_stats->cached_kB;
}
if (mem_stats->used_kB == INVAL_SIZE)
mem_stats->used_kB = mem_stats->total_kB - mem_stats->avail_kB;
}
int main(int argc, char **argv)
{
int result;
float warn_threshold;
float crit_threshold;
struct mem_stats stats;
float mem_pct;
const char *panic_str;
/* cmd line : $0 -w warn_threshold -c crit_threshold */
warn_threshold = strtof(argv[2], NULL);
crit_threshold = strtof(argv[4], NULL);
dump_mem_stats(&stats);
mem_pct = 10000 * stats.used_kB / stats.total_kB / 100.f;
panic_str = "OK";
result = 0;
if (mem_pct >= crit_threshold)
{
panic_str = "Critical";
result = 2;
}
else if (mem_pct >= warn_threshold)
{
panic_str = "Warning";
result = 1;
}
printf("MEMORY %s - Used = %.2f%% | 'Total'=%" PRIu64 "MB 'Used'=%" PRIu64 "MB 'Free'=%" PRIu64 "MB\n", panic_str, mem_pct, stats.total_kB / 1024, stats.used_kB / 1024, stats.avail_kB / 1024);
return result;
}