1 /*
2   Copyright 2020 Northern.tech AS
3 
4   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 3.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18 
19   To the extent this program is licensed as part of the Enterprise
20   versions of CFEngine, the applicable Commercial Open Source License
21   (COSL) may apply to this file if you as a licensee so wish it. See
22   included file COSL.txt.
23 */
24 
25 
26 #include <cf3.defs.h>
27 
28 #include <file_lib.h>
29 #include <monitoring.h>
30 #include <probes.h>
31 #include <proc_keyvalue.h>
32 
33 
34 /************************************************************************/
35 /* Generic "Key: Numeric Value" parser */
36 /************************************************************************/
37 
38 /* Getting data from /proc/meminfo -  All values are in bytes */
39 
40 typedef struct
41 {
42     off_t total;
43     off_t free;
44     off_t cached;
45     off_t swap;
46     off_t free_swap;
47 } MemoryInfo;
48 
49 #define KB 1024
50 
AcceptMemoryField(const char * field,off_t value,void * param)51 static bool AcceptMemoryField(const char *field, off_t value, void *param)
52 {
53     MemoryInfo *info = param;
54 
55     value *= KB;
56 
57     if (!strcmp(field, "MemTotal"))
58     {
59         info->total = value;
60     }
61     else if (!strcmp(field, "MemFree"))
62     {
63         info->free = value;
64     }
65     else if (!strcmp(field, "Cached"))
66     {
67         info->cached = value;
68     }
69     else if (!strcmp(field, "SwapTotal"))
70     {
71         info->swap = value;
72     }
73     else if (!strcmp(field, "SwapFree"))
74     {
75         info->free_swap = value;
76     }
77 
78     return true;
79 }
80 
81 /************************************************************************/
82 
MonMeminfoGatherData(double * cf_this)83 static void MonMeminfoGatherData(double *cf_this)
84 {
85     MemoryInfo info = { 0 };
86 
87     FILE *fh = safe_fopen("/proc/meminfo", "r");
88     if (fh == NULL)
89     {
90         Log(LOG_LEVEL_ERR, "Unable to open /proc/meminfo. (fopen: %s)", GetErrorStr());
91         return;
92     }
93 
94     if (ParseKeyNumericValue(fh, &AcceptMemoryField, &info))
95     {
96         int total_slot = NovaRegisterSlot(MON_MEM_TOTAL, "Total system memory", "megabytes",
97                                           512.0f, 4096.0f, true);
98         int free_slot = NovaRegisterSlot(MON_MEM_FREE, "Free system memory", "megabytes",
99                                          0.0f, 4096.0f, true);
100         int cached_slot = NovaRegisterSlot(MON_MEM_CACHED, "Size of disk cache", "megabytes",
101                                            0.0f, 4096.0f, true);
102         int swap_slot = NovaRegisterSlot(MON_MEM_SWAP, "Total swap size", "megabytes",
103                                          0.0f, 4096.0f, true);
104         int free_swap_slot = NovaRegisterSlot(MON_MEM_FREE_SWAP, "Free swap size", "megabytes",
105                                               0.0f, 8192.0f, true);
106 
107         if (total_slot != -1)
108         {
109             cf_this[total_slot] = ((double) info.total) / KB / KB;
110         }
111         if (free_slot != -1)
112         {
113             cf_this[free_slot] = ((double) info.free) / KB / KB;
114         }
115         if (cached_slot != -1)
116         {
117             cf_this[cached_slot] = ((double) info.cached) / KB / KB;
118         }
119         if (swap_slot != -1)
120         {
121             cf_this[swap_slot] = ((double) info.swap) / KB / KB;
122         }
123         if (free_swap_slot != -1)
124         {
125             cf_this[free_swap_slot] = ((double) info.free_swap) / KB / KB;
126         }
127     }
128     else
129     {
130         Log(LOG_LEVEL_ERR, "Unable to parse /proc/meminfo");
131     }
132 
133     fclose(fh);
134 }
135 
136 /************************************************************************/
137 
MonMemoryInit(const char ** name,const char ** error)138 ProbeGatherData MonMemoryInit(const char **name, const char **error)
139 {
140     if (access("/proc/meminfo", R_OK) == 0)
141     {
142         *name = "Linux /proc/meminfo statistics";
143         *error = NULL;
144         return &MonMeminfoGatherData;
145     }
146     else
147     {
148         *name = NULL;
149         *error = "/proc/meminfo is not readable";
150         return NULL;
151     }
152 }
153