add unix domain socket support

This commit is contained in:
lxsang 2021-02-03 13:24:48 +01:00
parent dc4f5e252d
commit 7850c941c7
3 changed files with 306 additions and 245 deletions

107
sysmon.c
View File

@ -14,20 +14,24 @@
#include <sys/time.h>
#include <sys/statvfs.h>
#include <math.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "ini.h"
#ifndef PREFIX
#define PREFIX
#endif
#define DEFAULT_CONF_FILE (PREFIX "/etc/sysmond.conf")
#define MODULE_NAME "sysmon"
// #define DEFAULT_INPUT "/sys/class/hwmon/hwmon2/device/in3_input"
#define NET_INF_STAT_PT "/sys/class/net/%s/statistics/%s"
#define LOG_INIT(m) do { \
#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__)
@ -67,6 +71,7 @@
#define MAX_BUF 256
#define EQU(a, b) (strncmp(a, b, MAX_BUF) == 0)
#define MAX_NETWORK_INF 8
typedef struct
{
char bat_in[MAX_BUF];
@ -86,7 +91,8 @@ typedef struct
uint16_t gpu;
} sys_temp_t;
typedef struct {
typedef struct
{
char name[32];
unsigned long tx;
unsigned long rx;
@ -94,7 +100,8 @@ typedef struct {
float tx_rate;
} sys_net_inf_t;
typedef struct {
typedef struct
{
uint8_t n_intf;
/*Monitor up to 8 interfaces*/
sys_net_inf_t interfaces[MAX_NETWORK_INF];
@ -107,7 +114,8 @@ typedef struct
float percent;
} sys_cpu_t;
typedef struct {
typedef struct
{
char mount_path[MAX_BUF];
unsigned long d_total;
unsigned long d_free;
@ -124,7 +132,8 @@ typedef struct
unsigned long m_swap_free;
} sys_mem_t;
typedef struct {
typedef struct
{
char conf_file[MAX_BUF];
char data_file_out[MAX_BUF];
sys_bat_t bat_stat;
@ -167,7 +176,9 @@ static void map(app_data_t* opt)
return;
}
float result = 101 - (101 / pow(1 + pow(1.33 * (volt - opt->bat_stat.min_voltage) /
(opt->bat_stat.max_voltage - opt->bat_stat.min_voltage), 4.5), 3));
(opt->bat_stat.max_voltage - opt->bat_stat.min_voltage),
4.5),
3));
if (result > 100.0)
result = 100.0;
@ -198,6 +209,27 @@ static int guard_write(int fd, void* buffer, size_t size)
return n;
}
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;
}
static int read_line(int fd, char *buf, int size)
{
int i = 0;
@ -265,7 +297,8 @@ static int read_cpu_info(app_data_t* opts)
while (token != NULL)
{
token = strtok(NULL, d);
if(token!=NULL){
if (token != NULL)
{
sum += strtoul(token, NULL, 10);
if (j == 3)
idle = strtoul(token, NULL, 10);
@ -303,7 +336,8 @@ static int read_mem_info(app_data_t* opts)
M_ERROR(MODULE_NAME, "Unable to open meminfo: %s", strerror(errno));
return -1;
}
for (int i = 0; i < 7; i++) {
for (int i = 0; i < 7; i++)
{
ret = read_line(fd, buf, MAX_BUF);
token = strtok(buf, d);
token = strtok(NULL, d);
@ -317,7 +351,8 @@ static int read_mem_info(app_data_t* opts)
}
if (i == 4)
{
for (int j = 0; j < 9; j++) {
for (int j = 0; j < 9; j++)
{
ret = read_line(fd, buf, MAX_BUF);
// skip 10 line
}
@ -333,7 +368,6 @@ static int read_mem_info(app_data_t* opts)
(void)ret;
(void)close(fd);
/*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,
@ -446,15 +480,32 @@ static int read_disk_usage(app_data_t* opts)
static int log_to_file(app_data_t *opts)
{
int ret,fd;
int ret, fd = -1, use_stdout = 0;
char out_buf[1024];
char net_buf[MAX_BUF];
(void)memset(out_buf, 0, sizeof(out_buf));
(void)memset(net_buf, 0, sizeof(net_buf));
if (opts->data_file_out[0] == '\0')
{
return 0;
}
// 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)
}
if (fd < 0 && !use_stdout)
{
M_ERROR(MODULE_NAME, "Unable to open output file: %s", strerror(errno));
return -1;
@ -463,7 +514,8 @@ static int log_to_file(app_data_t* opts)
char *ptr = buf;
// CPU
size_t len = 0;
for (int i = 0; i < opts->n_cpus; i++) {
for (int i = 0; i < opts->n_cpus; i++)
{
if (MAX_BUF - len - 1 <= 0)
{
break;
@ -477,7 +529,8 @@ static int log_to_file(app_data_t* opts)
// NET
len = 0;
ptr = net_buf;
for (int i = 0; i < opts->net.n_intf; i++) {
for (int i = 0; i < opts->net.n_intf; i++)
{
if (MAX_BUF - len - 1 < strlen(JSON_NET_FMT))
{
break;
@ -487,8 +540,7 @@ static int log_to_file(app_data_t* opts)
opts->net.interfaces[i].rx,
opts->net.interfaces[i].tx,
opts->net.interfaces[i].rx_rate,
opts->net.interfaces[i].tx_rate
);
opts->net.interfaces[i].tx_rate);
len = strlen(net_buf);
ptr = net_buf + len;
}
@ -515,8 +567,15 @@ static int log_to_file(app_data_t* opts)
opts->mem.m_swap_free,
opts->disk.d_total,
opts->disk.d_free,
net_buf
);
net_buf);
out_buf[strlen(out_buf)] = '\n';
ret = 0;
if (use_stdout)
{
printf("%s", out_buf);
}
else
{
ret = guard_write(fd, out_buf, strlen(out_buf));
if (ret <= 0)
{
@ -528,11 +587,8 @@ static int log_to_file(app_data_t* opts)
M_ERROR(MODULE_NAME, "Unable to write all battery info to output file");
ret = -1;
}
else
{
// M_LOG(MODULE_NAME, "written %d bytes to file: %s", strlen(out_buf), opts->data_file_out);
ret = 0;
}
if (fd > 0)
(void)close(fd);
return ret;
}
@ -646,7 +702,6 @@ static int load_config(app_data_t* opts)
opts->bat_stat.percent = 0.0;
opts->power_off_percent = 1;
(void)memset(&opts->mem, '\0', sizeof(opts->mem));
(void)memset(&opts->temp, '\0', sizeof(opts->temp));
(void)memset(&opts->net, '\0', sizeof(opts->net));

BIN
sysmond

Binary file not shown.

View File

@ -23,6 +23,7 @@ disk_mount_point = /
# the system will be shutdown after n count down
power_off_count_down = 10
# the system will bet shutdown if the battery voltage percent is below this value
# after `power_off_count_down` time
power_off_percent = 3
cpu_temperature_input=/sys/devices/virtual/thermal/thermal_zone1/temp
@ -30,4 +31,9 @@ cpu_temperature_input=/sys/devices/virtual/thermal/thermal_zone1/temp
gpu_temperature_input=/sys/devices/virtual/thermal/thermal_zone2/temp
# output system info to file
data_file_out = /var/jarvis:fbf070ddea3ea90d07f456540b405d302554ec82
# The output file may be: stdout, a regular file or name pipe, or a unix domain socket
# To print JSON data records to stdout use
# data_file_out = stdout
# To send data via unix domain socket use
# data_file_out = sock:/path/to/socket/file
data_file_out = sock:/var/sysmond.log