daeonizing

This commit is contained in:
Jonathan Santis 2024-09-16 10:33:55 +02:00
parent e173906cdf
commit 9fcaa5cfcb

68
main.c
View File

@ -11,19 +11,6 @@
#define HELP_TEXT "requires a command\n--\ngetSendStatus [objectpath]\nlistUnits\nconfig [configfile]\nset token with GOTIFY_TOKEN=\"sjdfoiasfd\"\n--\n"
int gotify_message(char *,char *,char *);
int systemdCall(DBusConnection *,char *, char *);
int checkIsActive(DBusConnection *connection, char *service_name);
int systemdUnitCall(DBusConnection *connection,char *target,char *method,char *param,char **result);
int GetSendStatus(DBusConnection *connection,char *objectpath);
int checkState(char *ServiceName,char *result);
/*
*sytemctl show gotify-server | grep PID
*sudo busctl tree org.freedesktop.systemd1 > get object paths
//org/freedesktop/systemd1
//gotify_message("https://5ccppi.org:4433/message?token=","anothertitle","anothermessage");
* */
typedef struct service
{
char *name;
@ -32,6 +19,22 @@ typedef struct service
int priority;
} Service;
int gotify_message(char *,char *,char *);
int systemdCall(DBusConnection *,char *, char *);
int checkIsActive(DBusConnection *connection, char *service_name);
int systemdUnitCall(DBusConnection *connection,char *target,char *method,char *param,char **result);
int GetSendStatus(DBusConnection *connection,char *objectpath);
int ServiceGetSendStatus(DBusConnection *connection,Service *service);
int checkState(char *ServiceName,char *result,Service *service);
/*
*sytemctl show gotify-server | grep PID
*sudo busctl tree org.freedesktop.systemd1 > get object paths
//org/freedesktop/systemd1
//gotify_message("https://5ccppi.org:4433/message?token=","anothertitle","anothermessage");
* */
int main(int argc,char *argv[])
{
DBusConnection *connection=NULL;
@ -96,7 +99,7 @@ int main(int argc,char *argv[])
printf("Running as a daemon\n");
char *str_entry[64]={'\0'};
int i=0;
Service services[64];
Service services[64]={'\0'};
char *ptr_name=NULL;
loadConfig(argv[2],str_entry,64);
@ -106,7 +109,6 @@ int main(int argc,char *argv[])
ptr_name = strrchr(services[i].targetPath,'/');
if(ptr_name!=NULL)
{
printf("strrchr:%s\n",ptr_name);
services[i].name = strdup(ptr_name);
services[i].name = services[i].name + 1;
}
@ -120,10 +122,10 @@ int main(int argc,char *argv[])
}
while(true)
{
for(i=0;str_entry[i];i++)
for(i=0;services[i].name;i++)
{
printf("entry in config:%s\n",str_entry[i]);
GetSendStatus(connection,str_entry[i]);
printf("entry in config:%s\n",services[i].name);
ServiceGetSendStatus(connection,&services[i]);
}
sleep(10);
}
@ -200,7 +202,21 @@ int GetSendStatus(DBusConnection *connection,char *objectpath)
int ret = systemdUnitCall(connection,objectpath,"Get","SubState",&result);
if(ret == 0)
{
checkState(objectpath,result);
//checkState(objectpath,result,NULL);
} else
{
fprintf(stderr,"could not call systemdunit\n");
}
return ret;
}
int ServiceGetSendStatus(DBusConnection *connection,Service *service)
{
char *result=0;
char *objectpath = service->targetPath;
int ret = systemdUnitCall(connection,objectpath,"Get","SubState",&result);
if(ret == 0)
{
checkState(service->name,result,service);
} else
{
fprintf(stderr,"could not call systemdunit\n");
@ -208,7 +224,7 @@ int GetSendStatus(DBusConnection *connection,char *objectpath)
return ret;
}
int checkState(char *ServiceName,char *result)
int checkState(char *ServiceName,char *result,Service *service)
{
int i=0;
char *error_strings[]={"dead","inactive","exited"};
@ -216,15 +232,21 @@ int checkState(char *ServiceName,char *result)
for(i=0;i<(sizeof(error_strings)/sizeof(error_strings[0]));i++)
{
//printf("%s %ld\n",error_strings[i],sizeof(error_strings)/sizeof(error_strings[0]));
if(strcmp(result,error_strings[i])==0)
if(strcmp(result,error_strings[i])==0 && service->alerted == false)
{
service->alerted=true;
gotify_message("https://5ccppi.org:4433",ServiceName,"is not running");
printf("!!system unit died!! Alert gotify server!\n");
}
else if(strcmp(result,"active")==0)
{
printf("!!service seems okay!!\n");
return 0;
if(service->alerted == true)
{
gotify_message("https://5ccppi.org:4433",ServiceName,"Running again, after crash");
}
service->alerted=false;
printf("!!service seems okay!!\n");
return 0;
}
}
return 1;