mirror of
https://github.com/EnergyMech/energymech.git
synced 2025-12-29 16:14:43 +00:00
random cumulative fixes/changes
This commit is contained in:
253
src/hostinfo.c
253
src/hostinfo.c
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
|
||||
EnergyMech, IRC bot software
|
||||
Copyright (c) 2018 proton
|
||||
Copyright (c) 2020 proton
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -65,6 +65,74 @@ struct /* statusvalues */
|
||||
{ NULL, 0, NULL }
|
||||
};
|
||||
|
||||
#ifdef DEBUG
|
||||
|
||||
struct
|
||||
{
|
||||
int value;
|
||||
char *str;
|
||||
|
||||
} in2str[] =
|
||||
{
|
||||
{ IN_ACCESS, "IN_ACCESS" }, /* File was accessed (read) */
|
||||
{ IN_ATTRIB, "IN_ATTRIB" }, /* Metadata changed, e.g., permissions, timestamps, extended attributes, link count, UID, GID, etc. */
|
||||
{ IN_CLOSE_WRITE, "IN_CLOSE_WRITE" }, /* File opened for writing was closed */
|
||||
{ IN_CLOSE_NOWRITE, "IN_CLOSE_NOWRITE" }, /* File not opened for writing was closed */
|
||||
{ IN_CREATE, "IN_CREATE" }, /* File/directory created in watched directory */
|
||||
{ IN_DELETE, "IN_DELETE" }, /* File/directory deleted from watched directory */
|
||||
{ IN_DELETE_SELF, "IN_DELETE_SELF" }, /* Watched file/directory was itself deleted */
|
||||
{ IN_MODIFY, "IN_MODIFY" }, /* File was modified */
|
||||
{ IN_MOVE_SELF, "IN_MOVE_SELF" }, /* Watched file/directory was itself moved */
|
||||
{ IN_MOVED_FROM, "IN_MOVED_FROM" }, /* Generated for the directory containing the old filename when a file is renamed */
|
||||
{ IN_MOVED_TO, "IN_MOVED_TO" }, /* Generated for the directory containing the new filename when a file is renamed */
|
||||
{ IN_OPEN, "IN_OPEN" }, /* File was opened */
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
char *inomask2str(uint32_t mask, char *dst)
|
||||
{
|
||||
const char *src;
|
||||
int i,n = 0;
|
||||
|
||||
for(i=0;in2str[i].str;i++)
|
||||
{
|
||||
if ((mask & in2str[i].value) == in2str[i].value)
|
||||
{
|
||||
if (n)
|
||||
dst[n++] = '|';
|
||||
src = in2str[i].str;
|
||||
for(;src[n];n++)
|
||||
dst[n] = src[n];
|
||||
}
|
||||
}
|
||||
dst[n] = 0;
|
||||
return(dst);
|
||||
}
|
||||
|
||||
#endif /* DEBUG */
|
||||
|
||||
int monitor_fs(const char *file)
|
||||
{
|
||||
FileMon *fmon,*fnew;
|
||||
int ino;
|
||||
|
||||
if ((ino = inotify_init()) < 0)
|
||||
return(-1);
|
||||
inotify_add_watch(ino,file,IN_ALL_EVENTS);
|
||||
#ifdef DEBUG
|
||||
debug("(monitor_fs) added notifier on %s [fd %i]\n",file,ino);
|
||||
#endif
|
||||
set_mallocdoer(monitor_fs);
|
||||
fnew = Calloc(sizeof(FileMon) + strlen(file));
|
||||
fnew->fd = ino;
|
||||
fnew->nospam = now;
|
||||
stringcpy(fnew->filename,file);
|
||||
|
||||
fnew->next = filemonlist;
|
||||
filemonlist = fnew;
|
||||
return(0);
|
||||
}
|
||||
|
||||
int parse_proc_status(char *line)
|
||||
{
|
||||
const char *key;
|
||||
@@ -182,6 +250,57 @@ int parse_proc_cpuinfo(char *line)
|
||||
return(FALSE); /* return false to continue reading lines */
|
||||
}
|
||||
|
||||
void select_monitor()
|
||||
{
|
||||
FileMon *fmon;
|
||||
|
||||
for(fmon=filemonlist;fmon;fmon=fmon->next)
|
||||
if (fmon->fd >= 0)
|
||||
{
|
||||
FD_SET(fmon->fd,&read_fds);
|
||||
chkhigh(fmon->fd);
|
||||
}
|
||||
}
|
||||
|
||||
void process_monitor()
|
||||
{
|
||||
FileMon *fmon;
|
||||
struct inotify_event *ivent;
|
||||
char tmp[256];
|
||||
int n;
|
||||
|
||||
for(fmon=filemonlist;fmon;fmon=fmon->next)
|
||||
{
|
||||
if (fmon->fd >= 0 && FD_ISSET(fmon->fd,&read_fds))
|
||||
{
|
||||
ivent = (struct inotify_event *)&globaldata;
|
||||
|
||||
n = read(fmon->fd,globaldata,sizeof(struct inotify_event));
|
||||
if (ivent->len > 0)
|
||||
read(fmon->fd,ivent->name,ivent->len);
|
||||
else
|
||||
*ivent->name = 0;
|
||||
#ifdef DEBUG
|
||||
debug("ino %i, n %i, sz %i\n",fmon->fd,n,sizeof(in2str));
|
||||
debug("wd %i, mask %lu, cookie %lu, len %lu, name %s\n",
|
||||
ivent->wd,ivent->mask,ivent->cookie,ivent->len,ivent->name);
|
||||
debug("%s\n",inomask2str(ivent->mask,tmp));
|
||||
debug("(process_monitor) ino %i bytes read, int wd = %i, uint32_t mask = %s (%lu), "
|
||||
"uint32_t cookie = %lu, uint32_t len = %lu, char name = %s\n",
|
||||
n,ivent->wd,inomask2str(ivent->mask,tmp),ivent->mask,ivent->cookie,ivent->len,ivent->name);
|
||||
#endif
|
||||
if ((ivent->mask & IN_CLOSE_WRITE) == IN_CLOSE_WRITE)
|
||||
return;
|
||||
if (fmon->nospam > now-30)
|
||||
return;
|
||||
fmon->nospam = now;
|
||||
send_global(SPYSTR_SYSMON,"Alert: file ``%s'' was touched",fmon->filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------------------------------------------------------------------------------*/
|
||||
|
||||
/*
|
||||
help:HOSTINFO:(no arguments)
|
||||
|
||||
@@ -191,10 +310,17 @@ See also: meminfo, cpuinfo
|
||||
*/
|
||||
void do_hostinfo(COMMAND_ARGS)
|
||||
{
|
||||
char *h,hostname[256];
|
||||
struct utsname un;
|
||||
|
||||
hostname[255] = 0;
|
||||
if (gethostname(hostname,250) < 0)
|
||||
h = "(hostname error)";
|
||||
else
|
||||
h = hostname;
|
||||
|
||||
if (uname(&un) == 0)
|
||||
to_user_q(from,"%s %s %s",un.sysname,un.release,un.machine);
|
||||
to_user_q(from,"%s %s %s %s",h,un.sysname,un.release,un.machine);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -240,6 +366,7 @@ void do_cpuinfo(COMMAND_ARGS)
|
||||
char bogostr[64],cpustr[64];
|
||||
char *a1,*a2,*a3,*dst;
|
||||
int fd,n;
|
||||
double loads[3];
|
||||
|
||||
#ifdef DEVELOPING
|
||||
a1 = chop(&rest);
|
||||
@@ -271,25 +398,7 @@ void do_cpuinfo(COMMAND_ARGS)
|
||||
omni[1] = 0;
|
||||
readline(fd,&parse_proc_cpuinfo); /* readline closes fd */
|
||||
|
||||
if ((fd = open("/proc/loadavg",O_RDONLY)) < 0)
|
||||
#ifdef DEBUG
|
||||
{
|
||||
debug("(do_cpuinfo) /proc/loadavg: %s\n",strerror(errno));
|
||||
return;
|
||||
}
|
||||
#else
|
||||
return;
|
||||
#endif
|
||||
n = read(fd,globaldata,MSGLEN-2);
|
||||
globaldata[n] = 0;
|
||||
close(fd);
|
||||
|
||||
rest = globaldata;
|
||||
a1 = chop(&rest);
|
||||
a2 = chop(&rest);
|
||||
a3 = chop(&rest);
|
||||
|
||||
if (!a3 || !*a3)
|
||||
if (getloadavg(loads,3) < 3)
|
||||
return;
|
||||
|
||||
#ifdef DEBUG
|
||||
@@ -313,103 +422,29 @@ void do_cpuinfo(COMMAND_ARGS)
|
||||
if (cores)
|
||||
sprintf(STREND(cpustr),", %i core%s",cores,(cores == 1) ? "" : "s");
|
||||
}
|
||||
to_user_q(from,"%s%s%s, loadavg: %s(1m) %s(5m) %s(15m)",
|
||||
omni+1,bogostr,cpustr,a1,a2,a3);
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------------------------------------------------------------------------------*/
|
||||
|
||||
#ifdef DEBUG
|
||||
|
||||
struct
|
||||
{
|
||||
int value;
|
||||
char *str;
|
||||
|
||||
} in2str[] =
|
||||
{
|
||||
{ IN_ACCESS, "IN_ACCESS" }, /* File was accessed (read) */
|
||||
{ IN_ATTRIB, "IN_ATTRIB" }, /* Metadata changed, e.g., permissions, timestamps, extended attributes, link count, UID, GID, etc. */
|
||||
{ IN_CLOSE_WRITE, "IN_CLOSE_WRITE" }, /* File opened for writing was closed */
|
||||
{ IN_CLOSE_NOWRITE, "IN_CLOSE_NOWRITE" }, /* File not opened for writing was closed */
|
||||
{ IN_CREATE, "IN_CREATE" }, /* File/directory created in watched directory */
|
||||
{ IN_DELETE, "IN_DELETE" }, /* File/directory deleted from watched directory */
|
||||
{ IN_DELETE_SELF, "IN_DELETE_SELF" }, /* Watched file/directory was itself deleted */
|
||||
{ IN_MODIFY, "IN_MODIFY" }, /* File was modified */
|
||||
{ IN_MOVE_SELF, "IN_MOVE_SELF" }, /* Watched file/directory was itself moved */
|
||||
{ IN_MOVED_FROM, "IN_MOVED_FROM" }, /* Generated for the directory containing the old filename when a file is renamed */
|
||||
{ IN_MOVED_TO, "IN_MOVED_TO" }, /* Generated for the directory containing the new filename when a file is renamed */
|
||||
{ IN_OPEN, "IN_OPEN" }, /* File was opened */
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
char *inomask2str(uint32_t mask, char *dst)
|
||||
{
|
||||
const char *src;
|
||||
int i,n = 0;
|
||||
|
||||
for(i=0;in2str[i].str;i++)
|
||||
{
|
||||
if ((mask & in2str[i].value) == in2str[i].value)
|
||||
{
|
||||
if (n)
|
||||
dst[n++] = '|';
|
||||
src = in2str[i].str;
|
||||
for(;src[n];n++)
|
||||
dst[n] = src[n];
|
||||
}
|
||||
}
|
||||
dst[n] = 0;
|
||||
return(dst);
|
||||
}
|
||||
|
||||
#endif /* DEBUG */
|
||||
|
||||
int ino;
|
||||
|
||||
void monitor_fs(const char *file)
|
||||
{
|
||||
struct inotify_event *ivent;
|
||||
|
||||
ino = inotify_init();
|
||||
inotify_add_watch(ino,file,IN_ALL_EVENTS);
|
||||
#ifdef DEBUG
|
||||
debug("(monitor_fs) added notifier on %s [fd %i]\n",file,ino);
|
||||
debug("(do_cpuinfo) %s%s%s, loadavg: %.2f(1m) %.2f(5m) %.2f(15m)",
|
||||
omni+1,bogostr,cpustr,(loads[0]),(loads[1]),(loads[2]));
|
||||
#endif
|
||||
to_user_q(from,"%s%s%s, loadavg: %.2f(1m) %.2f(5m) %.2f(15m)",
|
||||
omni+1,bogostr,cpustr,(loads[0]),(loads[1]),(loads[2]));
|
||||
}
|
||||
|
||||
void select_monitor()
|
||||
void do_filemon(COMMAND_ARGS)
|
||||
{
|
||||
FD_SET(ino,&read_fds);
|
||||
}
|
||||
struct stat st;
|
||||
|
||||
void process_monitor()
|
||||
{
|
||||
struct inotify_event *ivent;
|
||||
char tmp[256];
|
||||
int n;
|
||||
|
||||
if (FD_ISSET(ino,&read_fds))
|
||||
{
|
||||
ivent = (struct inotify_event *)&globaldata;
|
||||
|
||||
n = read(ino,globaldata,sizeof(struct inotify_event));
|
||||
if (ivent->len > 0)
|
||||
read(ino,ivent->name,ivent->len);
|
||||
else
|
||||
*ivent->name = 0;
|
||||
#ifdef DEBUG
|
||||
debug("ino %i, n %i, sz %i\n",ino,n,sizeof(in2str));
|
||||
debug("wd %i, mask %lu, cookie %lu, len %lu, name %s\n",
|
||||
ivent->wd,ivent->mask,ivent->cookie,ivent->len,ivent->name);
|
||||
debug("%s\n",inomask2str(ivent->mask,tmp));
|
||||
debug("(process_monitor) ino %i bytes read, int wd = %i, uint32_t mask = %s (%lu), uint32_t cookie = %lu, uint32_t len = %lu, char name = %s\n",
|
||||
n,ivent->wd,inomask2str(ivent->mask,tmp),ivent->mask,ivent->cookie,ivent->len,ivent->name);
|
||||
debug("(do_filemon) rest = '%s'\n",rest);
|
||||
#endif
|
||||
if ((ivent->mask & IN_CLOSE_WRITE) == IN_CLOSE_WRITE)
|
||||
return;
|
||||
send_global(SPYSTR_SYSMON,"Alert: Executable was touched");
|
||||
|
||||
if (stat(rest,&st) < 0 || monitor_fs(rest) < 0)
|
||||
{
|
||||
to_user_q(from,"Unable to add file monitor for: ``%s''",rest);
|
||||
return;
|
||||
}
|
||||
to_user_q(from,"File monitor added.");
|
||||
}
|
||||
|
||||
#endif /* HOSTINFO */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user