-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.c
182 lines (142 loc) · 4.98 KB
/
data.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
#include <stdio.h> // {,s,f}printf, fopen, fgets, fclose, fseek, SEEK_SET
#include <string.h> // strlen, strncmp, strtok, strcmp, memset
#include <stdbool.h> // true
#include <unistd.h> // gethostname
#include <time.h> // time, time_t, ctime_r
#include <stdlib.h> // strtol
#include "sysmond.h"
void get_data( char* out_string )
{
static bool first = true;
static FILE* loadavgFile;
static FILE* statFile;
static FILE* meminfoFile;
static FILE* uptimeFile;
static char hostname[ 64 ];
char buffer [ 48 ];
char buffer2 [ 64 ];
time_t now;
struct tm* info;
int dst;
static long previousWork;
static long previousTotal;
long user;
long nice;
long system;
long idle;
long work;
long total;
long MemTotal = 0; // Initialize to prevent a warning
long MemAvailable;
double percent;
if ( first )
{
// Get the host name for the output string
// This only needs to be done once
memset ( hostname, 0, 64 );
gethostname( hostname, 64 );
// Open files and populate initial values
// Load average
loadavgFile = fopen( "/proc/loadavg", "r" );
setvbuf( loadavgFile, NULL, _IONBF, 0 );
// System uptime
uptimeFile = fopen( "/proc/uptime", "r" );
setvbuf( uptimeFile, NULL, _IONBF, 0 );
// CPU usage
statFile = fopen( "/proc/stat", "r" );
setvbuf( statFile, NULL, _IONBF, 0 );
// We need to initialize previousWork and previousTotal
fgets( buffer, sizeof( buffer), statFile);
strtok( buffer, " " ); // Discard "cpu"
user = strtol( strtok( NULL, " " ), NULL, 10 );
nice = strtol( strtok( NULL, " " ), NULL, 10 );
system = strtol( strtok( NULL, " " ), NULL, 10 );
idle = strtol( strtok( NULL, " " ), NULL, 10 );
previousWork = user + nice + system;
previousTotal = previousWork + idle;
// Memory
meminfoFile = fopen( "/proc/meminfo", "r" );
setvbuf( meminfoFile, NULL, _IONBF, 0 );
// Don't do this again
first = false;
}
// Copy the hostname and a semi-colon
sprintf( out_string, "%s;", hostname );
// Get the current time -- seconds since Epoch and format
now = time( NULL );
ctime_r( &now, buffer );
buffer[ 24 ] = 0; // Remove \n
tzset(); // Get the time zone
info = gmtime( &now );
dst = ( info->tm_isdst > 0 ) ? 1 : 0;
sprintf( buffer2, "time:%s %s;", buffer, tzname[ dst ] );
strcat( out_string, buffer2 );
// Get the uptime
fseek( uptimeFile, 0, SEEK_SET );
fgets( buffer, sizeof( buffer), uptimeFile);
char* seconds = strtok( buffer, " " );
sprintf( buffer2, "uptime:%s;", seconds );
strcat( out_string, buffer2 );
// Get the load average - 1, 5, and 15 minute averages
fseek( loadavgFile, 0, SEEK_SET );
fgets( buffer, sizeof( buffer), loadavgFile);
for ( int i = 0, j = 0; i < strlen(buffer); i++ )
{
char buffer3[ 80 ];
if ( buffer[ i ] == ' ' ) j++;
if ( j < 3 ) continue;
memcpy( buffer2, buffer, i );
buffer2[ i ] = 0;
sprintf( buffer3, "load:%s;", buffer2 ); //
strcat( out_string, buffer3 );
break;
}
// Get CPU percentage
// Only need first line: "cpu" user nice system idle
// The last 4 are a count of "jiffies"
fseek( statFile, 0, SEEK_SET );
fgets( buffer, sizeof( buffer), statFile);
strtok( buffer, " " ); // Discard "cpu"
user = strtol( strtok( NULL, " " ), NULL, 10 );
nice = strtol( strtok( NULL, " " ), NULL, 10 );
system = strtol( strtok( NULL, " " ), NULL, 10 );
idle = strtol( strtok( NULL, " " ), NULL, 10 );
work = user + nice + system;
total = work + idle;
if ( total - previousTotal <= 0 )
percent = 0.0;
else
percent = (double) (work - previousWork) / ( total - previousTotal );
sprintf( buffer, "cpu%%:%4.2f;", percent ); // Note the semicolon here
strcat ( out_string, buffer );
previousWork = work;
previousTotal = total;
// Get the memory usage (MemTotal - MemAvailable) / MemTotal
fseek( meminfoFile, 0, SEEK_SET );
while( true )
{
char* cp;
fgets( buffer, sizeof( buffer), meminfoFile);
cp = strtok( buffer, " " );
strcpy( buffer2, cp );
if ( strcmp( buffer2, "MemTotal:" ) == 0 )
{
cp = strtok( NULL, " " );
strcpy( buffer2, cp );
MemTotal = strtol( buffer2, NULL, 10 );
continue;
}
if ( strcmp( buffer2, "MemAvailable:" ) == 0 )
{
cp = strtok( NULL, " " );
strcpy( buffer2, cp );
MemAvailable = strtol( buffer2, NULL, 10 );
break;
}
}
percent = ( (MemTotal - MemAvailable) * 100.0 ) / MemTotal;
sprintf( buffer, "mem%%:%4.2f\n", percent ); // Note the new line here
strcat ( out_string, buffer );
get_temps( out_string );
return;
}