2021-02-03 12:40:43 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/timerfd.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#include <math.h>
|
2021-02-03 13:24:48 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2021-02-03 12:40:43 +01:00
|
|
|
|
|
|
|
#include "ini.h"
|
2021-02-03 13:24:48 +01:00
|
|
|
#ifndef PREFIX
|
|
|
|
#define PREFIX
|
|
|
|
#endif
|
2021-02-03 12:40:43 +01:00
|
|
|
#define DEFAULT_CONF_FILE (PREFIX "/etc/sysmond.conf")
|
|
|
|
#define MODULE_NAME "sysmon"
|
|
|
|
#define NET_INF_STAT_PT "/sys/class/net/%s/statistics/%s"
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
#define LOG_INIT(m) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
setlogmask(LOG_UPTO(LOG_NOTICE)); \
|
|
|
|
openlog((m), LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define M_LOG(m, a, ...) syslog((LOG_NOTICE), m "_log@[%s: %d]: " a "\n", __FILE__, \
|
|
|
|
__LINE__, ##__VA_ARGS__)
|
|
|
|
|
|
|
|
#define M_ERROR(m, a, ...) syslog((LOG_ERR), m "_error@[%s: %d]: " a "\n", __FILE__, \
|
|
|
|
__LINE__, ##__VA_ARGS__)
|
|
|
|
|
|
|
|
#define JSON_FMT "{" \
|
|
|
|
"\"stamp_sec\": %lu," \
|
|
|
|
"\"stamp_usec\": %lu," \
|
|
|
|
"\"battery\": %.3f," \
|
|
|
|
"\"battery_percent\": %.3f," \
|
|
|
|
"\"battery_max_voltage\": %d," \
|
|
|
|
"\"battery_min_voltage\": %d," \
|
|
|
|
"\"cpu_temp\": %d," \
|
|
|
|
"\"gpu_temp\": %d," \
|
|
|
|
"\"cpu_usages\":[%s]," \
|
|
|
|
"\"mem_total\": %lu," \
|
|
|
|
"\"mem_free\": %lu," \
|
|
|
|
"\"mem_used\": %lu," \
|
|
|
|
"\"mem_buff_cache\": %lu," \
|
|
|
|
"\"mem_available\": %lu," \
|
|
|
|
"\"mem_swap_total\": %lu," \
|
|
|
|
"\"mem_swap_free\": %lu," \
|
|
|
|
"\"disk_total\": %lu," \
|
|
|
|
"\"disk_free\": %lu," \
|
|
|
|
"\"net\":[%s]" \
|
|
|
|
"}"
|
|
|
|
|
|
|
|
#define JSON_NET_FMT "{" \
|
|
|
|
"\"name\":\"%s\"," \
|
|
|
|
"\"rx\": %lu," \
|
|
|
|
"\"tx\": %lu," \
|
|
|
|
"\"rx_rate\": %.3f," \
|
|
|
|
"\"tx_rate\": %.3f" \
|
|
|
|
"},"
|
2021-02-03 12:40:43 +01:00
|
|
|
|
|
|
|
#define MAX_BUF 256
|
2021-02-03 13:24:48 +01:00
|
|
|
#define EQU(a, b) (strncmp(a, b, MAX_BUF) == 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
#define MAX_NETWORK_INF 8
|
2021-02-03 13:24:48 +01:00
|
|
|
|
2021-02-03 12:40:43 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char bat_in[MAX_BUF];
|
|
|
|
uint16_t max_voltage;
|
|
|
|
uint16_t min_voltage;
|
|
|
|
uint16_t cutoff_voltage;
|
|
|
|
float ratio;
|
|
|
|
uint16_t read_voltage;
|
|
|
|
float percent;
|
|
|
|
} sys_bat_t;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char cpu_temp_file[MAX_BUF];
|
|
|
|
char gpu_temp_file[MAX_BUF];
|
|
|
|
uint16_t cpu;
|
|
|
|
uint16_t gpu;
|
|
|
|
} sys_temp_t;
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
2021-02-03 12:40:43 +01:00
|
|
|
char name[32];
|
|
|
|
unsigned long tx;
|
|
|
|
unsigned long rx;
|
|
|
|
float rx_rate;
|
|
|
|
float tx_rate;
|
|
|
|
} sys_net_inf_t;
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
2021-02-03 12:40:43 +01:00
|
|
|
uint8_t n_intf;
|
|
|
|
/*Monitor up to 8 interfaces*/
|
|
|
|
sys_net_inf_t interfaces[MAX_NETWORK_INF];
|
|
|
|
} sys_net_t;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
unsigned long last_idle;
|
|
|
|
unsigned long last_sum;
|
|
|
|
float percent;
|
|
|
|
} sys_cpu_t;
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
2021-02-03 12:40:43 +01:00
|
|
|
char mount_path[MAX_BUF];
|
|
|
|
unsigned long d_total;
|
|
|
|
unsigned long d_free;
|
|
|
|
} sys_disk_t;
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
typedef struct
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
unsigned long m_total;
|
|
|
|
unsigned long m_free;
|
|
|
|
unsigned long m_available;
|
|
|
|
unsigned long m_cache;
|
|
|
|
unsigned long m_buffer;
|
|
|
|
unsigned long m_swap_total;
|
|
|
|
unsigned long m_swap_free;
|
|
|
|
} sys_mem_t;
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
2021-02-03 12:40:43 +01:00
|
|
|
char conf_file[MAX_BUF];
|
|
|
|
char data_file_out[MAX_BUF];
|
|
|
|
sys_bat_t bat_stat;
|
2021-02-03 13:24:48 +01:00
|
|
|
sys_cpu_t *cpus;
|
2021-02-03 12:40:43 +01:00
|
|
|
sys_mem_t mem;
|
|
|
|
sys_temp_t temp;
|
|
|
|
sys_net_t net;
|
|
|
|
sys_disk_t disk;
|
|
|
|
int n_cpus;
|
|
|
|
struct itimerspec sample_period;
|
|
|
|
int pwoff_cd;
|
|
|
|
uint8_t power_off_percent;
|
|
|
|
} app_data_t;
|
|
|
|
|
|
|
|
static volatile int running = 1;
|
|
|
|
static char buf[MAX_BUF];
|
|
|
|
|
|
|
|
static void int_handler(int dummy)
|
|
|
|
{
|
|
|
|
(void)dummy;
|
|
|
|
running = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void help(const char *app)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"Usage: %s options.\n"
|
|
|
|
"Options:\n"
|
|
|
|
"\t -f <value>: config file\n"
|
|
|
|
"\t -h <value>: this help message\n",
|
|
|
|
app);
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
static void map(app_data_t *opt)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
float volt = opt->bat_stat.read_voltage * opt->bat_stat.ratio;
|
|
|
|
if (volt < opt->bat_stat.min_voltage)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
opt->bat_stat.percent = 0.0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
float result = 101 - (101 / pow(1 + pow(1.33 * (volt - opt->bat_stat.min_voltage) /
|
2021-02-03 13:24:48 +01:00
|
|
|
(opt->bat_stat.max_voltage - opt->bat_stat.min_voltage),
|
|
|
|
4.5),
|
|
|
|
3));
|
|
|
|
if (result > 100.0)
|
2021-02-03 12:40:43 +01:00
|
|
|
result = 100.0;
|
2021-02-03 13:24:48 +01:00
|
|
|
|
2021-02-03 12:40:43 +01:00
|
|
|
opt->bat_stat.percent = result;
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
static int guard_write(int fd, void *buffer, size_t size)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
int write_len;
|
|
|
|
int st;
|
2021-02-03 13:24:48 +01:00
|
|
|
while (n != (int)size)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
write_len = (int)size - n;
|
2021-02-03 13:24:48 +01:00
|
|
|
st = write(fd, buffer + n, write_len);
|
|
|
|
if (st == -1)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
M_ERROR(MODULE_NAME, "Unable to write to #%d: %s", fd, strerror(errno));
|
2021-02-03 12:40:43 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
if (st == 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
M_ERROR(MODULE_NAME, "Endpoint %d is closed", fd);
|
2021-02-03 12:40:43 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
n += st;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
static int open_unix_socket(char *path)
|
|
|
|
{
|
|
|
|
struct sockaddr_un address;
|
|
|
|
address.sun_family = AF_UNIX;
|
|
|
|
(void)memset(address.sun_path, 0, sizeof(address.sun_path));
|
|
|
|
(void)memcpy(address.sun_path, path, sizeof(address.sun_path));
|
|
|
|
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to create Unix domain socket: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (connect(fd, (struct sockaddr *)(&address), sizeof(address)) == -1)
|
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to connect to socket '%s': %s", address.sun_path, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
M_LOG(MODULE_NAME, "Socket %s is created successfully", path);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2021-02-03 12:40:43 +01:00
|
|
|
static int read_line(int fd, char *buf, int size)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
char c = '\0';
|
|
|
|
int n;
|
|
|
|
while ((i < size - 1) && (c != '\n'))
|
|
|
|
{
|
|
|
|
n = read(fd, &c, 1);
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
buf[i] = c;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
c = '\n';
|
|
|
|
}
|
|
|
|
buf[i] = '\0';
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
static int read_voltage(app_data_t *opts)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
int fd, ret;
|
2021-02-03 13:24:48 +01:00
|
|
|
if (opts->bat_stat.bat_in[0] == '\0')
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fd = open(opts->bat_stat.bat_in, O_RDONLY);
|
2021-02-03 13:24:48 +01:00
|
|
|
if (fd < 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to open input: %s", opts->bat_stat.bat_in);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
(void)memset(buf, '\0', sizeof(buf));
|
|
|
|
ret = read(fd, buf, sizeof(buf));
|
2021-02-03 13:24:48 +01:00
|
|
|
if (ret > 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
opts->bat_stat.read_voltage = atoi(buf);
|
|
|
|
map(opts);
|
|
|
|
}
|
|
|
|
(void)close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
static int read_cpu_info(app_data_t *opts)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
int fd, ret, j, i = 0;
|
|
|
|
const char d[2] = " ";
|
2021-02-03 13:24:48 +01:00
|
|
|
char *token;
|
2021-02-03 12:40:43 +01:00
|
|
|
unsigned long sum = 0, idle = 0;
|
|
|
|
fd = open("/proc/stat", O_RDONLY);
|
2021-02-03 13:24:48 +01:00
|
|
|
if (fd < 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to open stat: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (i = 0; i < opts->n_cpus; i++)
|
|
|
|
{
|
|
|
|
ret = read_line(fd, buf, MAX_BUF);
|
2021-02-03 13:24:48 +01:00
|
|
|
if (ret > 0 && buf[0] == 'c' && buf[1] == 'p' && buf[2] == 'u')
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
token = strtok(buf, d);
|
2021-02-03 12:40:43 +01:00
|
|
|
sum = 0;
|
|
|
|
j = 0;
|
2021-02-03 13:24:48 +01:00
|
|
|
while (token != NULL)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
token = strtok(NULL, d);
|
|
|
|
if (token != NULL)
|
|
|
|
{
|
2021-02-03 12:40:43 +01:00
|
|
|
sum += strtoul(token, NULL, 10);
|
2021-02-03 13:24:48 +01:00
|
|
|
if (j == 3)
|
2021-02-03 12:40:43 +01:00
|
|
|
idle = strtoul(token, NULL, 10);
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
opts->cpus[i].percent = 100 - (idle - opts->cpus[i].last_idle) * 100.0 / (sum - opts->cpus[i].last_sum);
|
2021-02-03 12:40:43 +01:00
|
|
|
opts->cpus[i].last_idle = idle;
|
|
|
|
opts->cpus[i].last_sum = sum;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to read CPU infos at: %d", i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
(void)close(fd);
|
|
|
|
if (i == 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "No CPU info found");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
static int read_mem_info(app_data_t *opts)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
int fd, ret;
|
|
|
|
const char d[2] = " ";
|
|
|
|
unsigned long data[7];
|
2021-02-03 13:24:48 +01:00
|
|
|
char *token;
|
2021-02-03 12:40:43 +01:00
|
|
|
fd = open("/proc/meminfo", O_RDONLY);
|
2021-02-03 13:24:48 +01:00
|
|
|
if (fd < 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to open meminfo: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
for (int i = 0; i < 7; i++)
|
|
|
|
{
|
|
|
|
ret = read_line(fd, buf, MAX_BUF);
|
|
|
|
token = strtok(buf, d);
|
|
|
|
token = strtok(NULL, d);
|
|
|
|
if (token != NULL)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
data[i] = (unsigned long)strtoul(token, NULL, 10);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data[i] = 0;
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
if (i == 4)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
for (int j = 0; j < 9; j++)
|
|
|
|
{
|
|
|
|
ret = read_line(fd, buf, MAX_BUF);
|
2021-02-03 12:40:43 +01:00
|
|
|
// skip 10 line
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
opts->mem.m_total = data[0];
|
|
|
|
opts->mem.m_free = data[1];
|
|
|
|
opts->mem.m_available = data[2];
|
|
|
|
opts->mem.m_buffer = data[3];
|
|
|
|
opts->mem.m_cache = data[4];
|
|
|
|
opts->mem.m_swap_total = data[5];
|
|
|
|
opts->mem.m_swap_free = data[6];
|
2021-02-03 12:40:43 +01:00
|
|
|
(void)ret;
|
|
|
|
(void)close(fd);
|
2021-02-03 13:24:48 +01:00
|
|
|
|
2021-02-03 12:40:43 +01:00
|
|
|
/*printf("total: %d used: %d, free: %d buffer/cache: %d, available: %d \n",
|
|
|
|
opts->mem.m_total / 1024,
|
|
|
|
(opts->mem.m_total - opts->mem.m_free - opts->mem.m_buffer-opts->mem.m_cache)/1024,
|
|
|
|
opts->mem.m_free/1024,
|
|
|
|
(opts->mem.m_buffer+opts->mem.m_cache)/1024,
|
|
|
|
opts->mem.m_available/1024);*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
static int read_temp_file(const char *file, uint16_t *output)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
int fd, ret;
|
2021-02-03 13:24:48 +01:00
|
|
|
if (file[0] != '\0')
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
fd = open(file, O_RDONLY);
|
|
|
|
if (fd < 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to open temp file %s : %s", file, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
(void)memset(buf, '\0', sizeof(buf));
|
|
|
|
ret = read(fd, buf, MAX_BUF);
|
2021-02-03 13:24:48 +01:00
|
|
|
if (ret < 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to read temperature: %s", strerror(errno));
|
2021-02-03 13:24:48 +01:00
|
|
|
(void)close(fd);
|
2021-02-03 12:40:43 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*output = (uint16_t)atoi(buf);
|
2021-02-03 13:24:48 +01:00
|
|
|
(void)close(fd);
|
2021-02-03 12:40:43 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
static int read_cpu_temp(app_data_t *opts)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
if (read_temp_file(opts->temp.cpu_temp_file, &opts->temp.cpu) == -1)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return read_temp_file(opts->temp.gpu_temp_file, &opts->temp.gpu);
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
static int read_net_statistic(app_data_t *opts)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
int fd, ret;
|
|
|
|
float period;
|
|
|
|
long unsigned int bytes;
|
2021-02-03 13:24:48 +01:00
|
|
|
|
2021-02-03 12:40:43 +01:00
|
|
|
period = ((float)opts->sample_period.it_value.tv_nsec) / 1.0e9;
|
|
|
|
for (int i = 0; i < opts->net.n_intf; i++)
|
|
|
|
{
|
|
|
|
// rx
|
2021-02-03 13:24:48 +01:00
|
|
|
(void)snprintf(buf, MAX_BUF - 1, NET_INF_STAT_PT, opts->net.interfaces[i].name, "rx_bytes");
|
2021-02-03 12:40:43 +01:00
|
|
|
fd = open(buf, O_RDONLY);
|
2021-02-03 13:24:48 +01:00
|
|
|
if (fd < 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to open %s: %s", buf, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// read data to buff
|
2021-02-03 13:24:48 +01:00
|
|
|
(void)memset(buf, '\0', MAX_BUF);
|
2021-02-03 12:40:43 +01:00
|
|
|
ret = read(fd, buf, MAX_BUF);
|
|
|
|
(void)close(fd);
|
2021-02-03 13:24:48 +01:00
|
|
|
if (ret <= 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to read RX data of %s: %s", opts->net.interfaces[i].name, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
bytes = (unsigned long)strtoul(buf, NULL, 10);
|
2021-02-03 12:40:43 +01:00
|
|
|
opts->net.interfaces[i].rx_rate = ((float)(bytes - opts->net.interfaces[i].rx) / period);
|
|
|
|
opts->net.interfaces[i].rx = bytes;
|
2021-02-03 13:24:48 +01:00
|
|
|
|
|
|
|
(void)snprintf(buf, MAX_BUF - 1, NET_INF_STAT_PT, opts->net.interfaces[i].name, "tx_bytes");
|
2021-02-03 12:40:43 +01:00
|
|
|
fd = open(buf, O_RDONLY);
|
2021-02-03 13:24:48 +01:00
|
|
|
if (fd < 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to open %s: %s", buf, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// read data to buff
|
2021-02-03 13:24:48 +01:00
|
|
|
(void)memset(buf, '\0', MAX_BUF);
|
2021-02-03 12:40:43 +01:00
|
|
|
ret = read(fd, buf, MAX_BUF);
|
|
|
|
(void)close(fd);
|
2021-02-03 13:24:48 +01:00
|
|
|
if (ret <= 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to read TX data of %s: %s", opts->net.interfaces[i].name, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
bytes = (unsigned long)strtoul(buf, NULL, 10);
|
2021-02-03 12:40:43 +01:00
|
|
|
opts->net.interfaces[i].tx_rate = ((float)(bytes - opts->net.interfaces[i].tx) / period);
|
2021-02-03 13:24:48 +01:00
|
|
|
opts->net.interfaces[i].tx = bytes;
|
2021-02-03 12:40:43 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
static int read_disk_usage(app_data_t *opts)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
struct statvfs stat;
|
|
|
|
int ret = statvfs(opts->disk.mount_path, &stat);
|
2021-02-03 13:24:48 +01:00
|
|
|
if (ret < 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to query disk usage of %s: %s", opts->disk.mount_path, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
opts->disk.d_total = stat.f_blocks * stat.f_frsize;
|
|
|
|
opts->disk.d_free = stat.f_bfree * stat.f_frsize;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
static int log_to_file(app_data_t *opts)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
int ret, fd = -1, use_stdout = 0;
|
2021-02-03 12:40:43 +01:00
|
|
|
char out_buf[1024];
|
|
|
|
char net_buf[MAX_BUF];
|
2021-02-03 13:24:48 +01:00
|
|
|
(void)memset(out_buf, 0, sizeof(out_buf));
|
|
|
|
(void)memset(net_buf, 0, sizeof(net_buf));
|
|
|
|
if (opts->data_file_out[0] == '\0')
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
// check if we use stdout
|
|
|
|
if (strncmp(opts->data_file_out, "stdout", 6) == 0)
|
|
|
|
{
|
|
|
|
// out put to stdout
|
|
|
|
use_stdout = 1;
|
|
|
|
}
|
|
|
|
else if (strncmp(opts->data_file_out, "sock:", 5) == 0)
|
|
|
|
{
|
|
|
|
// Unix domain socket
|
|
|
|
fd = open_unix_socket(opts->data_file_out + 5);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// open regular file
|
|
|
|
fd = open(opts->data_file_out, O_CREAT | O_WRONLY | O_APPEND | O_NONBLOCK, 0644);
|
|
|
|
}
|
|
|
|
if (fd < 0 && !use_stdout)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to open output file: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
(void)memset(buf, '\0', MAX_BUF);
|
|
|
|
char *ptr = buf;
|
2021-02-03 12:40:43 +01:00
|
|
|
// CPU
|
|
|
|
size_t len = 0;
|
2021-02-03 13:24:48 +01:00
|
|
|
for (int i = 0; i < opts->n_cpus; i++)
|
|
|
|
{
|
|
|
|
if (MAX_BUF - len - 1 <= 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
snprintf(ptr, MAX_BUF - len - 1, "%.3f,", opts->cpus[i].percent);
|
2021-02-03 12:40:43 +01:00
|
|
|
len = strlen(buf);
|
2021-02-03 13:24:48 +01:00
|
|
|
ptr = buf + len;
|
2021-02-03 12:40:43 +01:00
|
|
|
}
|
|
|
|
buf[len - 1] = '\0';
|
2021-02-03 13:24:48 +01:00
|
|
|
|
2021-02-03 12:40:43 +01:00
|
|
|
// NET
|
|
|
|
len = 0;
|
|
|
|
ptr = net_buf;
|
2021-02-03 13:24:48 +01:00
|
|
|
for (int i = 0; i < opts->net.n_intf; i++)
|
|
|
|
{
|
|
|
|
if (MAX_BUF - len - 1 < strlen(JSON_NET_FMT))
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
snprintf(ptr, MAX_BUF - len - 1, JSON_NET_FMT,
|
|
|
|
opts->net.interfaces[i].name,
|
|
|
|
opts->net.interfaces[i].rx,
|
|
|
|
opts->net.interfaces[i].tx,
|
|
|
|
opts->net.interfaces[i].rx_rate,
|
|
|
|
opts->net.interfaces[i].tx_rate);
|
2021-02-03 12:40:43 +01:00
|
|
|
len = strlen(net_buf);
|
2021-02-03 13:24:48 +01:00
|
|
|
ptr = net_buf + len;
|
2021-02-03 12:40:43 +01:00
|
|
|
}
|
|
|
|
net_buf[len - 1] = '\0';
|
2021-02-03 13:24:48 +01:00
|
|
|
|
2021-02-03 12:40:43 +01:00
|
|
|
struct timeval now;
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
snprintf(out_buf, sizeof(out_buf), JSON_FMT,
|
2021-02-03 13:24:48 +01:00
|
|
|
now.tv_sec,
|
|
|
|
now.tv_usec,
|
|
|
|
opts->bat_stat.read_voltage * opts->bat_stat.ratio,
|
|
|
|
opts->bat_stat.percent,
|
|
|
|
opts->bat_stat.max_voltage,
|
|
|
|
opts->bat_stat.min_voltage,
|
|
|
|
opts->temp.cpu,
|
|
|
|
opts->temp.gpu,
|
|
|
|
buf,
|
|
|
|
opts->mem.m_total,
|
|
|
|
opts->mem.m_free,
|
|
|
|
(opts->mem.m_total - opts->mem.m_free - opts->mem.m_buffer - opts->mem.m_cache),
|
|
|
|
opts->mem.m_buffer + opts->mem.m_cache,
|
|
|
|
opts->mem.m_available,
|
|
|
|
opts->mem.m_swap_total,
|
|
|
|
opts->mem.m_swap_free,
|
|
|
|
opts->disk.d_total,
|
|
|
|
opts->disk.d_free,
|
|
|
|
net_buf);
|
|
|
|
out_buf[strlen(out_buf)] = '\n';
|
|
|
|
ret = 0;
|
|
|
|
if (use_stdout)
|
|
|
|
{
|
|
|
|
printf("%s", out_buf);
|
2021-02-03 12:40:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
ret = guard_write(fd, out_buf, strlen(out_buf));
|
|
|
|
if (ret <= 0)
|
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to write data to output file");
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
if (ret != (int)strlen(out_buf))
|
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to write all battery info to output file");
|
|
|
|
ret = -1;
|
|
|
|
}
|
2021-02-03 12:40:43 +01:00
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
if (fd > 0)
|
|
|
|
(void)close(fd);
|
2021-02-03 12:40:43 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ini_handle(void *user_data, const char *section, const char *name, const char *value)
|
|
|
|
{
|
|
|
|
(void)section;
|
|
|
|
unsigned long period = 0;
|
|
|
|
const char d[2] = ",";
|
2021-02-03 13:24:48 +01:00
|
|
|
char *token;
|
|
|
|
|
|
|
|
app_data_t *opts = (app_data_t *)user_data;
|
|
|
|
if (EQU(name, "battery_max_voltage"))
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
opts->bat_stat.max_voltage = atoi(value);
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
else if (EQU(name, "battery_min_voltage"))
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
opts->bat_stat.min_voltage = atoi(value);
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
else if (EQU(name, "battery_cutoff_votalge"))
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
opts->bat_stat.cutoff_voltage = atoi(value);
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
else if (EQU(name, "battery_divide_ratio"))
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
opts->bat_stat.ratio = atof(value);
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
else if (EQU(name, "battery_input"))
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
strncpy(opts->bat_stat.bat_in, value, MAX_BUF - 1);
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
else if (EQU(name, "sample_period"))
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
period = strtoul(value, NULL, 10) * 1e6;
|
2021-02-03 12:40:43 +01:00
|
|
|
opts->sample_period.it_interval.tv_nsec = period;
|
|
|
|
opts->sample_period.it_value.tv_nsec = period;
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
else if (EQU(name, "cpu_core_number"))
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
opts->n_cpus = atoi(value) + 1;
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
else if (EQU(name, "power_off_count_down"))
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
opts->pwoff_cd = atoi(value);
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
else if (EQU(name, "power_off_percent"))
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
opts->power_off_percent = (uint8_t)atoi(value);
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
else if (EQU(name, "data_file_out"))
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
(void)strncpy(opts->data_file_out, value, MAX_BUF - 1);
|
2021-02-03 12:40:43 +01:00
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
else if (EQU(name, "cpu_temperature_input"))
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
(void)strncpy(opts->temp.cpu_temp_file, value, MAX_BUF - 1);
|
2021-02-03 12:40:43 +01:00
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
else if (EQU(name, "gpu_temperature_input"))
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
(void)strncpy(opts->temp.gpu_temp_file, value, MAX_BUF - 1);
|
2021-02-03 12:40:43 +01:00
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
else if (EQU(name, "disk_mount_point"))
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
(void)strncpy(opts->disk.mount_path, value, MAX_BUF - 1);
|
2021-02-03 12:40:43 +01:00
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
else if (EQU(name, "network_interfaces"))
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
// parsing the network interfaces
|
2021-02-03 13:24:48 +01:00
|
|
|
token = strtok((char *)value, d);
|
2021-02-03 12:40:43 +01:00
|
|
|
opts->net.n_intf = 0;
|
2021-02-03 13:24:48 +01:00
|
|
|
while (token != NULL)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
(void)strncpy(opts->net.interfaces[opts->net.n_intf].name, token, sizeof(opts->net.interfaces[opts->net.n_intf].name) - 1);
|
2021-02-03 12:40:43 +01:00
|
|
|
opts->net.n_intf++;
|
2021-02-03 13:24:48 +01:00
|
|
|
if (opts->net.n_intf >= MAX_NETWORK_INF)
|
2021-02-03 12:40:43 +01:00
|
|
|
break;
|
2021-02-03 13:24:48 +01:00
|
|
|
token = strtok(NULL, d);
|
2021-02-03 12:40:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Ignore unknown configuration %s = %s", name, value);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
|
2021-02-03 12:40:43 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:24:48 +01:00
|
|
|
static int load_config(app_data_t *opts)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
// global
|
|
|
|
(void)memset(opts->data_file_out, '\0', MAX_BUF);
|
|
|
|
(void)memset(opts->temp.cpu_temp_file, '\0', MAX_BUF);
|
|
|
|
(void)memset(opts->temp.gpu_temp_file, '\0', MAX_BUF);
|
|
|
|
opts->pwoff_cd = 5;
|
|
|
|
opts->sample_period.it_interval.tv_sec = 0;
|
|
|
|
opts->sample_period.it_interval.tv_nsec = 3e+8;
|
|
|
|
opts->sample_period.it_value.tv_sec = 0;
|
|
|
|
opts->sample_period.it_value.tv_nsec = 3e+8;
|
|
|
|
opts->cpus = NULL;
|
|
|
|
opts->n_cpus = 2;
|
2021-02-03 13:24:48 +01:00
|
|
|
|
2021-02-03 12:40:43 +01:00
|
|
|
//battery
|
|
|
|
(void)memset(opts->bat_stat.bat_in, '\0', MAX_BUF);
|
|
|
|
opts->bat_stat.max_voltage = 4200;
|
|
|
|
opts->bat_stat.min_voltage = 3300;
|
|
|
|
opts->bat_stat.cutoff_voltage = 3000;
|
|
|
|
opts->bat_stat.ratio = 1.0;
|
|
|
|
opts->bat_stat.read_voltage = 0.0;
|
|
|
|
opts->bat_stat.percent = 0.0;
|
|
|
|
opts->power_off_percent = 1;
|
2021-02-03 13:24:48 +01:00
|
|
|
|
2021-02-03 12:40:43 +01:00
|
|
|
(void)memset(&opts->mem, '\0', sizeof(opts->mem));
|
|
|
|
(void)memset(&opts->temp, '\0', sizeof(opts->temp));
|
|
|
|
(void)memset(&opts->net, '\0', sizeof(opts->net));
|
|
|
|
(void)memset(&opts->disk, '\0', sizeof(opts->disk));
|
|
|
|
opts->disk.mount_path[0] = '/';
|
2021-02-03 13:24:48 +01:00
|
|
|
|
2021-02-03 12:40:43 +01:00
|
|
|
M_LOG(MODULE_NAME, "Use configuration: %s", opts->conf_file);
|
|
|
|
if (ini_parse(opts->conf_file, ini_handle, opts) < 0)
|
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Can't load '%s'", opts->conf_file);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// check battery configuration
|
2021-02-03 13:24:48 +01:00
|
|
|
if ((opts->bat_stat.max_voltage < opts->bat_stat.min_voltage) ||
|
2021-02-03 12:40:43 +01:00
|
|
|
(opts->bat_stat.max_voltage < opts->bat_stat.cutoff_voltage) ||
|
|
|
|
(opts->bat_stat.min_voltage < opts->bat_stat.cutoff_voltage))
|
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Battery configuration is invalid: max: %d, min: %d, cut off: %d",
|
2021-02-03 13:24:48 +01:00
|
|
|
opts->bat_stat.max_voltage,
|
|
|
|
opts->bat_stat.min_voltage,
|
|
|
|
opts->bat_stat.cutoff_voltage);
|
2021-02-03 12:40:43 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *const *argv)
|
|
|
|
{
|
|
|
|
int ret, tfd, count_down;
|
|
|
|
float volt;
|
|
|
|
uint64_t expirations_count;
|
|
|
|
app_data_t opts;
|
|
|
|
LOG_INIT(MODULE_NAME);
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
signal(SIGABRT, SIG_IGN);
|
|
|
|
signal(SIGINT, int_handler);
|
|
|
|
(void)strncpy(opts.conf_file, DEFAULT_CONF_FILE, MAX_BUF - 1);
|
|
|
|
while ((ret = getopt(argc, argv, "hf:")) != -1)
|
|
|
|
{
|
|
|
|
switch (ret)
|
|
|
|
{
|
|
|
|
case 'f':
|
2021-02-03 13:24:48 +01:00
|
|
|
(void)strncpy(opts.conf_file, optarg, MAX_BUF - 1);
|
2021-02-03 12:40:43 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
help(argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
|
|
|
|
if (optind > argc)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
help(argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
|
|
|
|
if (load_config(&opts) != 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
fprintf(stderr, "Unable to read config file\n");
|
2021-02-03 12:40:43 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
|
2021-02-03 12:40:43 +01:00
|
|
|
M_LOG(MODULE_NAME, "Data Output: %s", opts.data_file_out);
|
|
|
|
M_LOG(MODULE_NAME, "Battery input: %s", opts.bat_stat.bat_in);
|
|
|
|
M_LOG(MODULE_NAME, "Battery Max voltage: %d", opts.bat_stat.max_voltage);
|
|
|
|
M_LOG(MODULE_NAME, "Battery Min voltage: %d", opts.bat_stat.min_voltage);
|
|
|
|
M_LOG(MODULE_NAME, "Battery Cut off voltage: %d", opts.bat_stat.cutoff_voltage);
|
|
|
|
M_LOG(MODULE_NAME, "Battery Divide ratio: %.3f", opts.bat_stat.ratio);
|
|
|
|
M_LOG(MODULE_NAME, "Sample period: %d", (int)(opts.sample_period.it_value.tv_nsec / 1e6));
|
|
|
|
M_LOG(MODULE_NAME, "CPU cores: %d", opts.n_cpus);
|
|
|
|
M_LOG(MODULE_NAME, "Power off count down: %d", opts.pwoff_cd);
|
2021-02-03 13:24:48 +01:00
|
|
|
M_LOG(MODULE_NAME, "CPU temp. input: %s", opts.temp.cpu_temp_file);
|
|
|
|
M_LOG(MODULE_NAME, "GPU temp. input: %s", opts.temp.gpu_temp_file);
|
2021-02-03 12:40:43 +01:00
|
|
|
M_LOG(MODULE_NAME, "Poweroff percent: %d", opts.power_off_percent);
|
2021-02-03 13:24:48 +01:00
|
|
|
|
2021-02-03 12:40:43 +01:00
|
|
|
// init timerfd
|
|
|
|
tfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
|
|
|
|
if (tfd == -1)
|
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to create timerfd: %s", strerror(errno));
|
2021-02-03 13:24:48 +01:00
|
|
|
fprintf(stderr, "Unable to create timer fd: %s\n", strerror(errno));
|
2021-02-03 12:40:43 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (timerfd_settime(tfd, 0 /* no flags */, &opts.sample_period, NULL) == -1)
|
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to set framerate period: %s", strerror(errno));
|
|
|
|
(void)close(tfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
//init CPU monitors
|
2021-02-03 13:24:48 +01:00
|
|
|
opts.cpus = (sys_cpu_t *)malloc(opts.n_cpus * sizeof(sys_cpu_t));
|
|
|
|
for (int i = 0; i < opts.n_cpus; i++)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
opts.cpus[i].last_sum = 0;
|
|
|
|
opts.cpus[i].last_idle = 0;
|
|
|
|
opts.cpus[i].percent = 0.0;
|
|
|
|
}
|
|
|
|
// loop
|
|
|
|
count_down = opts.pwoff_cd;
|
2021-02-03 13:24:48 +01:00
|
|
|
while (running)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
if (opts.bat_stat.bat_in[0] != '\0')
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
// open the file
|
2021-02-03 13:24:48 +01:00
|
|
|
if (read_voltage(&opts) == -1)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to read system voltage");
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
volt = opts.bat_stat.read_voltage * opts.bat_stat.ratio;
|
|
|
|
if (volt < opts.bat_stat.cutoff_voltage)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_LOG(MODULE_NAME, "Invalid voltage read: %.3f", volt);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-03 13:24:48 +01:00
|
|
|
if (opts.bat_stat.percent <= (float)opts.power_off_percent)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
count_down--;
|
|
|
|
M_LOG(MODULE_NAME, "Out of battery. Will shutdown after %d count down", count_down);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// reset the count_down
|
|
|
|
count_down = opts.pwoff_cd;
|
|
|
|
}
|
|
|
|
// check if we should shutdown
|
2021-02-03 13:24:48 +01:00
|
|
|
if (count_down <= 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_LOG(MODULE_NAME, "Shutting down system");
|
|
|
|
ret = system("poweroff");
|
2021-02-03 13:24:48 +01:00
|
|
|
(void)ret;
|
2021-02-03 12:40:43 +01:00
|
|
|
// this should never happend
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// read cpu info
|
2021-02-03 13:24:48 +01:00
|
|
|
if (read_cpu_info(&opts) == -1)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to read CPU infos");
|
|
|
|
}
|
|
|
|
// read memory usage
|
2021-02-03 13:24:48 +01:00
|
|
|
if (read_mem_info(&opts) == -1)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to read memory usage");
|
|
|
|
}
|
|
|
|
// read CPU temperature
|
2021-02-03 13:24:48 +01:00
|
|
|
if (read_cpu_temp(&opts) == -1)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to read CPU temperature");
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
if (read_net_statistic(&opts) == -1)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to query network statistic");
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
if (read_disk_usage(&opts) == -1)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to query disk usage");
|
|
|
|
}
|
|
|
|
// log to file
|
2021-02-03 13:24:48 +01:00
|
|
|
if (log_to_file(&opts) == -1)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to write sysinfo to output");
|
|
|
|
}
|
|
|
|
// check timeout
|
2021-02-03 13:24:48 +01:00
|
|
|
if (read(tfd, &expirations_count, sizeof(expirations_count)) != (int)sizeof(expirations_count))
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "Unable to read timer: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
else if (expirations_count > 1u)
|
|
|
|
{
|
|
|
|
M_ERROR(MODULE_NAME, "LOOP OVERFLOW COUNT: %lu", (long unsigned int)expirations_count);
|
|
|
|
}
|
|
|
|
}
|
2021-02-03 13:24:48 +01:00
|
|
|
|
|
|
|
if (opts.cpus)
|
2021-02-03 12:40:43 +01:00
|
|
|
free(opts.cpus);
|
2021-02-03 13:24:48 +01:00
|
|
|
if (tfd > 0)
|
2021-02-03 12:40:43 +01:00
|
|
|
{
|
|
|
|
(void)close(tfd);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|