-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_sysinfo.c
392 lines (324 loc) · 12.1 KB
/
pg_sysinfo.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include "postgres.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/statvfs.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/sysinfo.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/resource.h>
#include "postmaster/bgworker.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/proc.h"
#include "storage/shmem.h"
#include "storage/spin.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/timestamp.h"
#include "utils/builtins.h"
#include "utils/array.h"
#include "utils/elog.h"
#include "funcapi.h"
#include "pg_sysinfo.h"
PG_MODULE_MAGIC;
typedef struct {
char* name;
int total_size;
int used_size;
int free_size;
} sysinfo_data;
void _PG_init(void);
Datum sysinfo_os_name(PG_FUNCTION_ARGS);
Datum sysinfo_os_release(PG_FUNCTION_ARGS);
Datum sysinfo_os_version(PG_FUNCTION_ARGS);
Datum sysinfo_cpu(PG_FUNCTION_ARGS);
Datum sysinfo_disk(PG_FUNCTION_ARGS);
Datum sysinfo_ram(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(my_worker_task);
PG_FUNCTION_INFO_V1(sysinfo_os_name);
PG_FUNCTION_INFO_V1(sysinfo_os_release);
PG_FUNCTION_INFO_V1(sysinfo_os_version);
PG_FUNCTION_INFO_V1(sysinfo_cpu);
PG_FUNCTION_INFO_V1(sysinfo_cpu_usage);
PG_FUNCTION_INFO_V1(sysinfo_disk);
PG_FUNCTION_INFO_V1(sysinfo_ram);
void _PG_init(void)
{
/* Init */
}
/* Returns the name of the operating system. */
Datum sysinfo_os_name(PG_FUNCTION_ARGS)
{
struct utsname utsname;
char os_name[256];
if (uname(&utsname) != 0) {
ereport(ERROR,
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
errmsg("Failed to retrieve OS information")));
}
snprintf(os_name, sizeof(os_name), "%s", utsname.sysname);
PG_RETURN_TEXT_P(cstring_to_text(os_name));
}
/* Returns the release of the operating system. */
Datum sysinfo_os_release(PG_FUNCTION_ARGS)
{
struct utsname utsname;
char os_release[256];
if (uname(&utsname) != 0) {
ereport(ERROR,
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
errmsg("Failed to retrieve OS information")));
}
snprintf(os_release, sizeof(os_release), "%s", utsname.release);
PG_RETURN_TEXT_P(cstring_to_text(os_release));
}
/* Returns the version of the operating system. */
Datum sysinfo_os_version(PG_FUNCTION_ARGS)
{
struct utsname utsname;
char os_version[256];
if (uname(&utsname) != 0) {
ereport(ERROR,
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
errmsg("Failed to retrieve OS information")));
}
snprintf(os_version, sizeof(os_version), "%s", utsname.version);
PG_RETURN_TEXT_P(cstring_to_text(os_version));
}
/*
* sysinfo_ram - a PostgreSQL function to retrieve RAM usage information
*
* This function uses the sysinfo() function to retrieve the total, used, and free RAM.
* It then builds a tuple with three Float8 values, representing the total, used, and free RAM in gigabytes (GB).
*
* Parameters:
* - None
*
* Returns:
* - A tuple with three Float8 values: total RAM, used RAM, and free RAM (in GB)
*
* Throws:
* - ERROR if failed to retrieve RAM information or create composite result type
*/
Datum sysinfo_ram(PG_FUNCTION_ARGS)
{
/* Define variables */
struct sysinfo si;
double total_ram, free_ram, used_ram;
Datum values[3];
bool nulls[3] = { false, false, false };
TupleDesc tupleDesc;
HeapTuple tuple;
Datum result;
/* Retrieve RAM information */
if (sysinfo(&si) != 0) {
ereport(ERROR, (errmsg("Failed to retrieve RAM information")));
}
/* Calculate used and free RAM */
total_ram = (long)si.totalram * si.mem_unit;
free_ram = (long)si.freeram * si.mem_unit;
used_ram = total_ram - free_ram;
/* Build the tuple descriptor */
if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE) {
ereport(ERROR, (errmsg("Unable to create composite result type")));
}
/* Set the values */
values[0] = Float8GetDatumFast(total_ram / (1024 * 1024 * 1024));
values[1] = Float8GetDatumFast(used_ram / (1024 * 1024 * 1024));
values[2] = Float8GetDatumFast(free_ram / (1024 * 1024 * 1024));
/* Build the tuple */
tuple = heap_form_tuple(tupleDesc, values, nulls);
/* Make the result */
result = HeapTupleGetDatum(tuple);
/* Return the result */
PG_RETURN_DATUM(result);
}
/* Returns information about disk space usage. */
Datum sysinfo_disk(PG_FUNCTION_ARGS)
{
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
TupleDesc tupdesc;
Datum values[4];
bool nulls[4] = {false};
double total_size = 0;
double used_size = 0;
double free_size = 0;
Tuplestorestate *tupstore;
MemoryContext per_query_ctx;
MemoryContext oldcontext;
struct statvfs fsInfo;
int result = 0;
FILE *mtabFile;
char device[256], mountPoint[256];
/* Switch into long-lived context to construct returned data structures */
per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
oldcontext = MemoryContextSwitchTo(per_query_ctx);
/* Build a tuple descriptor for our result type */
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
elog(ERROR, "return type must be a row type");
tupstore = tuplestore_begin_heap(true, false, 1024);
/* Build the result tuple */
rsinfo->returnMode = SFRM_Materialize;
rsinfo->setResult = tupstore;
rsinfo->setDesc = tupdesc;
result = statvfs("/", &fsInfo); // Get the root filesystem info
if (result == 0)
{
mountPoint[0] = '/'; mountPoint[1] = 0;
total_size = fsInfo.f_blocks * fsInfo.f_frsize;
free_size = fsInfo.f_bfree * fsInfo.f_frsize;
used_size = (fsInfo.f_blocks - fsInfo.f_bfree) * fsInfo.f_frsize;
values[0] = CStringGetTextDatum(mountPoint);
values[1] = Float8GetDatumFast(total_size/ (1024*1024*1024));
values[2] = Float8GetDatumFast(used_size /(1024*1024*1024));
values[3] = Float8GetDatumFast(free_size / (1024*1024*1024));
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
}
mtabFile = fopen("/etc/mtab", "r"); // Open /etc/mtab file to get all mounted filesystems
if (mtabFile != NULL)
{
while (!feof(mtabFile)) {
result = fscanf(mtabFile, "%255s %255s", device, mountPoint);
if (result == 2)
{
result = statvfs(mountPoint, &fsInfo);
if (result == 0)
{
total_size = fsInfo.f_blocks * fsInfo.f_frsize;
free_size = fsInfo.f_bfree * fsInfo.f_frsize;
used_size = (fsInfo.f_blocks - fsInfo.f_bfree) * fsInfo.f_frsize;
values[0] = CStringGetTextDatum(mountPoint);
values[1] = Float8GetDatumFast(total_size / (1024*1024*1024));
values[2] = Float8GetDatumFast(used_size / (1024*1024*1024));
values[3] = Float8GetDatumFast(free_size / (1024*1024*1024));
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
}
}
}
fclose(mtabFile);
}
tuplestore_donestoring(tupstore);
/* Switch back to the original context before returning */
MemoryContextSwitchTo(oldcontext);
return (Datum) 0;
}
Datum
sysinfo_cpu_usage(PG_FUNCTION_ARGS)
{
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
TupleDesc tupdesc;
Datum values[3];
bool nulls[3] = {false};
double user_time;
double system_time;
double total_time;
struct rusage usage;
Tuplestorestate *tupstore;
MemoryContext per_query_ctx;
MemoryContext oldcontext;
/* Switch into long-lived context to construct returned data structures */
per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
oldcontext = MemoryContextSwitchTo(per_query_ctx);
/* Build a tuple descriptor for our result type */
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
elog(ERROR, "return type must be a row type");
if (getrusage(RUSAGE_SELF, &usage) != 0) {
perror("Error calling getrusage");
return 1;
}
user_time = (double) usage.ru_utime.tv_sec + (double) usage.ru_utime.tv_usec / 1000000.0;
system_time = (double) usage.ru_stime.tv_sec + (double) usage.ru_stime.tv_usec / 1000000.0;
total_time = user_time + system_time;
tupstore = tuplestore_begin_heap(true, false, 1024);
/* Build the result tuple */
rsinfo->returnMode = SFRM_Materialize;
rsinfo->setResult = tupstore;
rsinfo->setDesc = tupdesc;
values[0] = Float8GetDatumFast(user_time);
values[1] = Float8GetDatumFast(system_time);
values[2] = Float8GetDatumFast(total_time);
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
tuplestore_donestoring(tupstore);
MemoryContextSwitchTo(oldcontext);
return (Datum) 0;
}
Datum
sysinfo_cpu(PG_FUNCTION_ARGS)
{
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
TupleDesc tupdesc;
Datum values[3];
bool nulls[3] = {false};
int ncpus;
int ncores;
char *cpu_make;
FILE *cpuinfo;
char line[1024];
Tuplestorestate *tupstore;
MemoryContext per_query_ctx;
MemoryContext oldcontext;
cpuinfo = fopen("/proc/cpuinfo", "r");
if (cpuinfo == NULL)
ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION), errmsg("Failed to open /proc/cpuinfo")));
/* Switch into long-lived context to construct returned data structures */
per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
oldcontext = MemoryContextSwitchTo(per_query_ctx);
/* Build a tuple descriptor for our result type */
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
elog(ERROR, "return type must be a row type");
ncpus = 0;
ncores = 0;
cpu_make = NULL;
while (fgets(line, sizeof(line), cpuinfo) != NULL) {
char *name = NULL;
char *value = NULL;
if (sscanf(line, "%m[^:]:%ms", &name, &value) == 2) {
/* Remove leading and trailing whitespace from name and value */
char *p = name + strlen(name) - 1;
while (isspace(*p) && p > name) {
*p-- = '\0';
}
p = value + strlen(value) - 1;
while (isspace(*p) && p > value) {
*p-- = '\0';
}
if (strcmp(name, "model name") == 0) {
/* This is the CPU make */
cpu_make = value;
} else if (strcmp(name, "processor") == 0) {
/* This is a logical CPU */
ncpus++;
} else if (strcmp(name, "core id") == 0) {
/* This is a core within a physical CPU */
ncores++;
}
/* Free memory allocated by sscanf */
free(name);
free(value);
}
}
fclose(cpuinfo);
tupstore = tuplestore_begin_heap(true, false, 1024);
/* Build the result tuple */
rsinfo->returnMode = SFRM_Materialize;
rsinfo->setResult = tupstore;
rsinfo->setDesc = tupdesc;
values[0] = cpu_make == NULL ? (Datum) 0 : CStringGetTextDatum(cpu_make);
values[1] = Int32GetDatum(ncpus);
values[2] = Int32GetDatum(ncores);
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
tuplestore_donestoring(tupstore);
MemoryContextSwitchTo(oldcontext);
return (Datum) 0;
}