add host file modification\n cleaning string
This commit is contained in:
parent
bc85d7da5b
commit
14f1ab7398
17
config.c
17
config.c
@ -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)
|
||||
|
2
config.h
2
config.h
@ -1,2 +1,2 @@
|
||||
|
||||
int loadConfig(char *file, char **str_entry,int size);
|
||||
int loadConfig(char *file, char **str_entry,char **host,int size);
|
||||
|
20
main.c
20
main.c
@ -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,8 +128,11 @@ 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);
|
||||
}
|
||||
if(services[0].name)
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
for(i=0;services[i].name;i++)
|
||||
@ -134,6 +142,10 @@ int main(int argc,char *argv[])
|
||||
}
|
||||
sleep(10);
|
||||
}
|
||||
}else
|
||||
{
|
||||
fprintf(stderr,"No service found in config file, please check %s\n",path);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -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");
|
||||
|
4
makefile
4
makefile
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user