From 0a24537719de7cab1b4df46e7d6a22564f66ec6a Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Wed, 3 May 2017 13:23:21 +0100 Subject: Added new commands RUSAGE,LOADAVG --- Makefile | 5 ++++- cmd_loadavg.c | 28 ++++++++++++++++++++++++++++ cmd_loadavg.h | 11 +++++++++++ cmd_rusage.c | 34 ++++++++++++++++++++++++++++++++++ cmd_rusage.h | 14 ++++++++++++++ cmd_uptime.c | 25 +++++++++++++++++++++++-- cmd_version.c | 7 ++++++- config_cmds.h | 4 ++++ 8 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 cmd_loadavg.c create mode 100644 cmd_loadavg.h create mode 100644 cmd_rusage.c create mode 100644 cmd_rusage.h diff --git a/Makefile b/Makefile index 26ac9a8..7660b37 100644 --- a/Makefile +++ b/Makefile @@ -17,8 +17,11 @@ make: $(CC) $(CFLAGS) cmd_uptime.c -c $(CC) $(CFLAGS) cmd_version.c -c $(CC) $(CFLAGS) cmd_ping.c -c + $(CC) $(CFLAGS) cmd_loadavg.c -c + $(CC) $(CFLAGS) cmd_rusage.c -c $(CC) $(CFLAGS) tbl_qcmd.o mq_cmd.o mq_ntf.o buf.o mmm.o darray.o util.o sock_conn.o \ - irc_parse.o mmm.c cmd_date.o cmd_uptime.o cmd_version.o cmd_ping.o agni.c \ + irc_parse.o mmm.c cmd_date.o cmd_uptime.o cmd_version.o cmd_ping.o cmd_loadavg.o \ + cmd_rusage.c agni.c \ -o $(PROJECT) -std=c11 -lrt leak: diff --git a/cmd_loadavg.c b/cmd_loadavg.c new file mode 100644 index 0000000..a9a4b28 --- /dev/null +++ b/cmd_loadavg.c @@ -0,0 +1,28 @@ +#include "cmd_loadavg.h" + +void *cmd_loadavg(void *data) +{ + char *param = (char *)data; + char *ret = NULL; + int fret=-1; + + const int buf_size = 128; + char buf[buf_size+1]; + + double loadavg_d[3]; + + printf("LOADAVG\n"); + + fret = getloadavg(loadavg_d, 3); + if (fret<0) + { + ret = alloc_new_str("Cant get load avg mate\n"); + } + + printf("1M %lf 5M %lf 15M %lf\n", loadavg_d[0], loadavg_d[1], loadavg_d[2]); + snprintf(buf, buf_size,"1M %.1f 5M %.1f 15M %.1f\n", loadavg_d[0], loadavg_d[1], loadavg_d[2]); + + ret = alloc_new_str(buf); + + return ret; +} \ No newline at end of file diff --git a/cmd_loadavg.h b/cmd_loadavg.h new file mode 100644 index 0000000..d3bd9c2 --- /dev/null +++ b/cmd_loadavg.h @@ -0,0 +1,11 @@ +#ifndef __CMD_LOADAVG_H +#define __CMD_LOADAVG_H + +#include +#include + +#include "util.h" + +void *cmd_loadavg(void *data); + +#endif \ No newline at end of file diff --git a/cmd_rusage.c b/cmd_rusage.c new file mode 100644 index 0000000..3dc5d21 --- /dev/null +++ b/cmd_rusage.c @@ -0,0 +1,34 @@ + +#include "cmd_rusage.h" + +void *cmd_rusage(void *data) +{ + char *ret = NULL; + int fret=-1; + + const int buf_size = 128; + char buf[buf_size+1]; + + struct rusage cur_proc_rusage; + + printf("RUSAGE\n"); + + fret = getrusage(RUSAGE_SELF, &cur_proc_rusage); + if (fret<0) + { + ret = alloc_new_str("Cant get rusage mate\n"); + } + + printf("USER %ld SYS %ld\n", + cur_proc_rusage.ru_utime.tv_sec, + cur_proc_rusage.ru_stime.tv_sec + ); + snprintf(buf, buf_size, "USER %ld SYS %ld\n", + cur_proc_rusage.ru_utime.tv_sec, + cur_proc_rusage.ru_stime.tv_sec + ); + + ret = alloc_new_str(buf); + + return ret; +} diff --git a/cmd_rusage.h b/cmd_rusage.h new file mode 100644 index 0000000..5e35317 --- /dev/null +++ b/cmd_rusage.h @@ -0,0 +1,14 @@ +#ifndef __CMD_RUSAGE_H +#define __CMD_RUSAGE_H + +#include +#include + +#include +#include + +#include "util.h" + +void *cmd_rusage(void *data); + +#endif \ No newline at end of file diff --git a/cmd_uptime.c b/cmd_uptime.c index f745be0..285aec1 100644 --- a/cmd_uptime.c +++ b/cmd_uptime.c @@ -1,13 +1,34 @@ #include "cmd_uptime.h" +#define PROCFS_PATH "/proc" +#define PROCFS_UPTIME_PATH PROCFS_PATH "/uptime" + void *cmd_uptime(void *data) { char *param = (char *)data; char *ret = NULL; + const int buf_size = 128; + char buf[buf_size+1]; + printf("UPTIME\n"); - - ret = alloc_new_str("UpTime is infinite\n"); + + FILE *f=NULL; + double d1,d2; + int i1,i2; + + f = fopen(PROCFS_UPTIME_PATH,"r"); + fscanf(f,"%lf %lf", &d1, &d2); + fclose(f); + + printf("Readed %lf %lf\n", d1, d2); + i1 = d1; + i2 = d2; + + printf("Days %d Hours %d Minutes %d\n",i1/(3600*24),i1/(3600)%24, (i1/60)%60); + snprintf(buf, buf_size,"Days %d Hours %d Minutes %d\n",i1/(3600*24),i1/(3600)%24, (i1/60)%60); + + ret = alloc_new_str(buf); return ret; } \ No newline at end of file diff --git a/cmd_version.c b/cmd_version.c index d449ea1..a2190ab 100644 --- a/cmd_version.c +++ b/cmd_version.c @@ -2,5 +2,10 @@ void *cmd_version(void *data) { - return NULL; + char *param = (char *)data; + char *ret = NULL; + + printf("VERSION\n"); + + ret = alloc_new_str("v0.0.1\n"); } \ No newline at end of file diff --git a/config_cmds.h b/config_cmds.h index 2ae8574..125708b 100644 --- a/config_cmds.h +++ b/config_cmds.h @@ -13,6 +13,8 @@ typedef struct single_cmd_def #include "cmd_uptime.h" #include "cmd_version.h" #include "cmd_ping.h" +#include "cmd_loadavg.h" +#include "cmd_rusage.h" single_cmd_def confgi_cmd_list[] = { @@ -20,6 +22,8 @@ single_cmd_def confgi_cmd_list[] = {"UPTIME",cmd_uptime}, {"VERSION",cmd_version}, {"PING",cmd_ping}, + {"LOADAVG",cmd_loadavg}, + {"RUSE",cmd_rusage}, {NULL,NULL} }; -- cgit v1.2.3