add host file modification\n cleaning string

This commit is contained in:
Jonathan Santis 2024-09-17 08:33:53 +02:00
parent bc85d7da5b
commit 14f1ab7398
4 changed files with 37 additions and 20 deletions

View File

@ -3,7 +3,7 @@
#include<stdlib.h>
#include<string.h>
int loadConfig(char *str_file, char **str_entry,int size)
int loadConfig(char *str_file, char **str_entry,char **host,int size)
{
int i=0;
FILE *f_config;
@ -16,15 +16,19 @@ int loadConfig(char *str_file, char **str_entry,int size)
while(fgets(temp_str,256,f_config)!=NULL)
{
temp_str[strcspn(temp_str,"\n")]=0;
printf("while\n");
if(strcmp(temp_str,"[Host]")==0)
{
if(fgets(temp_str,256,f_config)!=NULL)
{
char host[256];
char *ptr_host = strrchr(temp_str,'=')+1;
strncpy(host,ptr_host,256);
printf("my host is:%s\n",host);
char *ptr_host = strrchr(temp_str,'=')+1;//Without =
ptr_host[strcspn(ptr_host,"\n")]=0;
while(ptr_host[0]==' ')
{
ptr_host = ptr_host + 1;
}
*host = strdup(ptr_host);
//strncpy(host,ptr_host,256);
printf("my host is:%s\n",*host);
}
}
else if(strcmp(temp_str,"[Services]")==0)
@ -57,6 +61,7 @@ int loadConfig(char *str_file, char **str_entry,int size)
{
fprintf(stderr,"error opening file[%s]\n",str_file);
}
printf("Done reading config file\n");
return 0;
}
/*int parseConfig(File *file, char *str_entry)

View File

@ -1,2 +1,2 @@
int loadConfig(char *file, char **str_entry,int size);
int loadConfig(char *file, char **str_entry,char **host,int size);

34
main.c
View File

@ -16,6 +16,7 @@ typedef struct service
{
char *name;
char *targetPath;
char *host;
bool alerted;
int priority;
} Service;
@ -87,9 +88,10 @@ int main(int argc,char *argv[])
if(argc==3 && strcmp(argv[2],"-d")!=0)
{
char *str_entry[64]={'\0'};
char *host=NULL;
int i=0;
loadConfig(argv[2],str_entry,64);
loadConfig(argv[2],str_entry,&host,64);
for(i=0;str_entry[i];i++)
{
printf("entry in config:%s\n",str_entry[i]);
@ -104,10 +106,13 @@ int main(int argc,char *argv[])
Service services[64]={'\0'};
char *ptr_name=NULL;
char path[256];
char *host=NULL;
getPath(path,256);
printf("[main]configPath:%s\n",path);
loadConfig(path,str_entry,64);
loadConfig(path,str_entry,&host,64);
printf("[main]host:%s\n",host);
for(i=0;str_entry[i];i++)
{
services[i].targetPath = strdup(str_entry[i]);
@ -123,16 +128,23 @@ int main(int argc,char *argv[])
}
services[i].alerted = false;
services[i].priority = 6;
services[i].host = host;
printf("Name:%s\ntarget:%s\n",services[i].name,services[i].targetPath);
}
while(true)
if(services[0].name)
{
for(i=0;services[i].name;i++)
while(true)
{
printf("entry in config:%s\n",services[i].name);
ServiceGetSendStatus(connection,&services[i]);
for(i=0;services[i].name;i++)
{
printf("entry in config:%s\n",services[i].name);
ServiceGetSendStatus(connection,&services[i]);
}
sleep(10);
}
sleep(10);
}else
{
fprintf(stderr,"No service found in config file, please check %s\n",path);
}
}
else
@ -142,12 +154,12 @@ int main(int argc,char *argv[])
}
else
{
fprintf(stderr,"Unknown command\n%s",HELP_TEXT);
fprintf(stderr,"Unknown command\n%s",HELP_TEXT);
}
}
else
{
fprintf(stderr,HELP_TEXT);
fprintf(stderr,HELP_TEXT);
}
return 0;
}
@ -239,14 +251,14 @@ int checkState(char *ServiceName,char *result,Service *service)
if(strcmp(result,error_strings[i])==0 && service->alerted == false)
{
service->alerted=true;
gotify_message("https://5ccppi.org:4433",ServiceName,"is not running");
gotify_message(service->host,ServiceName,"is not running");
printf("!!system unit died!! Alert gotify server!\n");
}
else if(strcmp(result,"active")==0 || strcmp(result,"running")==0)
{
if(service->alerted == true)
{
gotify_message("https://5ccppi.org:4433",ServiceName,"Running again, after crash");
gotify_message(service->host,ServiceName,"Running again, after crash");
}
service->alerted=false;
printf("!!service seems okay!!\n");

View File

@ -2,8 +2,8 @@ default:
gcc main.c config.c -lcurl -Wall -I/usr/include/dbus-1.0 -I/usr/lib/dbus-1.0/include -ldbus-1 -o systemd-gotify.bin
install:
sudo cp ./systemd-gotify.bin /usr/local/bin/systemd-gotify
mkdir $${HOME}/.config/systemd-gotify/
cp config $${HOME}/.config/systemd-gotify/config
mkdir -p $${HOME}/.config/systemd-gotify/
cp -i config $${HOME}/.config/systemd-gotify/config
uninstall:
sudo rm /usr/local/bin/systemd-gotify
rm $${HOME}/.config/systemd-gotify/config