1 /*
2   Copyright 2021 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 #include <cf3.defs.h>
26 
27 #include <file_lib.h>
28 #include <mon.h>
29 
30 #if !defined(__MINGW32__)
31 
32 /* Constants */
33 
34 # define MON_CPU_MAX 4
35 
36 /* Globals */
37 
38 static double LAST_CPU_Q[MON_CPU_MAX + 1] = { 0.0 };
39 static long LAST_CPU_T[MON_CPU_MAX + 1] = { 0 };
40 
41 /* Implementation */
42 
MonCPUGatherData(double * cf_this)43 void MonCPUGatherData(double *cf_this)
44 {
45     double q, dq;
46     char cpuname[CF_MAXVARSIZE], buf[CF_BUFSIZE];
47     long cpuidx, userticks = 0, niceticks = 0, systemticks = 0, idle = 0, iowait = 0, irq = 0, softirq = 0;
48     long total_time = 1;
49     enum observables slot = ob_spare;
50 
51     FILE *fp = safe_fopen("/proc/stat", "r");
52     if (fp == NULL)
53     {
54         Log(LOG_LEVEL_VERBOSE, "Could not open /proc/stat while gathering CPU data (fopen: %s)", GetErrorStr());
55         return;
56     }
57 
58     Log(LOG_LEVEL_VERBOSE, "Reading /proc/stat utilization data -------");
59 
60     while (!feof(fp))
61     {
62         if (fgets(buf, sizeof(buf), fp) == NULL)
63         {
64             break;
65         }
66         nt_static_assert(CF_MAXVARSIZE == 1024);
67         if (sscanf(buf, "%1023s%ld%ld%ld%ld%ld%ld%ld", cpuname, &userticks, &niceticks, &systemticks, &idle, &iowait, &irq,
68                &softirq) != 8)
69         {
70             Log(LOG_LEVEL_VERBOSE, "Could not scan /proc/stat line: %60s", buf);
71             continue;
72         }
73 
74         total_time = (userticks + niceticks + systemticks + idle);
75 
76         q = 100.0 * (double) (total_time - idle);
77 
78         if (strcmp(cpuname, "cpu") == 0)
79         {
80             Log(LOG_LEVEL_VERBOSE, "Found aggregate CPU");
81             slot = ob_cpuall;
82             cpuidx = MON_CPU_MAX;
83         }
84         else if (strncmp(cpuname, "cpu", 3) == 0)
85         {
86             if (sscanf(cpuname, "cpu%ld", &cpuidx) == 1)
87             {
88                 if ((cpuidx < 0) || (cpuidx >= MON_CPU_MAX))
89                 {
90                     continue;
91                 }
92             }
93             else
94             {
95                 continue;
96             }
97             slot = ob_cpu0 + cpuidx;
98         }
99         else
100         {
101             Log(LOG_LEVEL_VERBOSE, "Found nothing (%s)", cpuname);
102             slot = ob_spare;
103             fclose(fp);
104             return;
105         }
106 
107         dq = (q - LAST_CPU_Q[cpuidx]) / (double) (total_time - LAST_CPU_T[cpuidx]);     /* % Utilization */
108 
109         if ((dq > 100) || (dq < 0)) // Counter wrap around
110         {
111             dq = 50;
112         }
113 
114         cf_this[slot] = dq;
115         LAST_CPU_Q[cpuidx] = q;
116         LAST_CPU_T[cpuidx] = total_time;
117 
118         Log(LOG_LEVEL_VERBOSE, "Set %s=%d to %.1lf after %ld 100ths of a second ",
119             OBSERVABLES[slot][1], slot, cf_this[slot], total_time);
120     }
121 
122     fclose(fp);
123 }
124 
125 #endif /* !__MINGW32__ */
126