systemd-gotify/main.c

566 lines
20 KiB
C
Raw Normal View History

2024-09-04 07:28:24 +00:00
#include<stdio.h>
2024-09-05 12:48:23 +00:00
#include<stdlib.h>
2024-09-04 07:28:24 +00:00
#include<curl/curl.h>
#include<string.h>
#include<dbus/dbus.h>
2024-09-06 11:44:12 +00:00
#include<stdbool.h>
2024-09-11 15:45:08 +00:00
#include<time.h>
2024-09-16 07:51:17 +00:00
#include<unistd.h>
#include<pwd.h>
2024-09-11 15:45:08 +00:00
2024-09-11 07:43:33 +00:00
#include"config.h"
#define HELP_TEXT "requires a command\n--\ngetSendStatus [objectpath]\nlistUnits\nconfig [configfile / -d]\nset token with GOTIFY_TOKEN=\"sjdfoiasfd\"\n--\n"
2024-09-11 09:14:30 +00:00
2024-09-16 08:33:55 +00:00
typedef struct service
{
char *name;
char *targetPath;
char *host;
2024-09-16 08:33:55 +00:00
bool alerted;
int priority;
} Service;
2024-09-04 07:28:24 +00:00
int gotify_message(char *,char *,char *);
int systemdCall(DBusConnection *,char *, char *);
int checkIsActive(DBusConnection *connection, char *service_name);
2024-09-05 12:48:23 +00:00
int systemdUnitCall(DBusConnection *connection,char *target,char *method,char *param,char **result);
2024-09-06 12:57:16 +00:00
int GetSendStatus(DBusConnection *connection,char *objectpath);
2024-09-16 08:33:55 +00:00
int ServiceGetSendStatus(DBusConnection *connection,Service *service);
int checkState(char *ServiceName,char *result,Service *service);
int getPath(char *configPath,int size);
2024-09-04 07:28:24 +00:00
/*
*sytemctl show gotify-server | grep PID
2024-09-06 11:44:12 +00:00
*sudo busctl tree org.freedesktop.systemd1 > get object paths
2024-09-06 12:57:16 +00:00
//org/freedesktop/systemd1
//gotify_message("https://5ccppi.org:4433/message?token=","anothertitle","anothermessage");
2024-09-04 07:28:24 +00:00
* */
2024-09-16 08:33:55 +00:00
2024-09-06 11:44:12 +00:00
int main(int argc,char *argv[])
2024-09-04 07:28:24 +00:00
{
DBusConnection *connection=NULL;
DBusError error;
2024-09-11 15:45:08 +00:00
clock_t time;
char *str_entry[64]={'\0'};
char *host=NULL;
int i=0;
int intervall=10;
Service services[64]={'\0'};
char *ptr_name=NULL;
char path[256];
2024-09-11 15:45:08 +00:00
time = clock();
curl_global_init(CURL_GLOBAL_DEFAULT);
2024-09-11 15:45:08 +00:00
printf("time:%f\n",(float)time / CLOCKS_PER_SEC);
2024-09-04 07:28:24 +00:00
dbus_error_init(&error);
2024-09-06 11:44:12 +00:00
connection = dbus_bus_get(DBUS_BUS_SYSTEM,&error);//DBUS_BUS_SYSTEM DBUS_BUS_SESSION
2024-09-04 07:28:24 +00:00
if(dbus_error_is_set(&error))
{
fprintf(stderr,"%s",error.message);
dbus_error_free(&error);
2024-09-04 07:28:24 +00:00
return -1;
}
printf("This is my unique dbus name: %s\n",dbus_bus_get_unique_name(connection));
2024-09-06 12:57:16 +00:00
if(argc>1)
{
if(strcmp(argv[1],"getSendStatus")==0)
{
if(argc>2)
{
if(argv[2][0]=='/')
{
GetSendStatus(connection,argv[2]);
}
else
{
fprintf(stderr,"\n must be in the form of /org.freedesktop.systemd1/...\n");
return 1;
}
}
else
{
fprintf(stderr,"%s needs 1 argument\n",argv[1]);
return 2;
}
}
else if(strcmp(argv[1],"listUnits")==0)
{
systemdCall(connection,"ListUnits",NULL);
}
2024-09-11 09:14:30 +00:00
else if(strcmp(argv[1],"config")==0)
{
if(argc==3 && strcmp(argv[2],"-d")!=0)
2024-09-11 09:14:30 +00:00
{
loadConfig(argv[2],str_entry,&host,&intervall,64);
printf("[main]intervall:%d\n",intervall);
2024-09-11 09:14:30 +00:00
for(i=0;str_entry[i];i++)
{
printf("entry in config:%s\n",str_entry[i]);
2024-09-11 09:31:04 +00:00
GetSendStatus(connection,str_entry[i]);
2024-09-11 09:14:30 +00:00
}
}
else if(argc==3 && strcmp(argv[2],"-d")==0)
2024-09-16 07:51:17 +00:00
{
printf("Running as a daemon\n");
getPath(path,256);
printf("[main]configPath:%s\n",path);
2024-09-16 07:51:17 +00:00
loadConfig(path,str_entry,&host,&intervall,64);
printf("[main]host:%s\n",host);
printf("[main]intervall:%d\n",intervall);
2024-09-16 07:51:17 +00:00
for(i=0;str_entry[i];i++)
{
services[i].targetPath = strdup(str_entry[i]);
ptr_name = strrchr(services[i].targetPath,'/');
if(ptr_name!=NULL)
{
services[i].name = strdup(ptr_name);
services[i].name = services[i].name + 1;
}
else{
fprintf(stderr,"does not seem to be a valid target\n");
services[i].name = strdup("Undefined");
}
services[i].alerted = false;
services[i].priority = 6;
services[i].host = host;
2024-09-16 07:51:17 +00:00
printf("Name:%s\ntarget:%s\n",services[i].name,services[i].targetPath);
}
if(services[0].name)
2024-09-16 07:51:17 +00:00
{
while(true)
2024-09-16 07:51:17 +00:00
{
for(i=0;services[i].name;i++)
{
printf("entry in config:%s\n",services[i].name);
ServiceGetSendStatus(connection,&services[i]);
}
sleep(intervall);
2024-09-16 07:51:17 +00:00
}
}else
{
fprintf(stderr,"No service found in config file, please check %s\n",path);
2024-09-16 07:51:17 +00:00
}
}
2024-09-11 09:14:30 +00:00
else
{
2024-09-16 07:51:17 +00:00
fprintf(stderr,"config requires a config file as argument\nAppend -d for daemonizing\n");
2024-09-11 09:14:30 +00:00
}
}
2024-09-06 12:57:16 +00:00
else
{
fprintf(stderr,"Unknown command\n%s",HELP_TEXT);
2024-09-06 12:57:16 +00:00
}
}
else
{
fprintf(stderr,HELP_TEXT);
2024-09-06 12:57:16 +00:00
}
curl_global_cleanup();
2024-09-06 12:57:16 +00:00
return 0;
}
int getPath(char *configPath,int size)
{
uid_t myid = getuid();
printf("My uid:%d\n",myid);
struct passwd *info = getpwuid(myid);
printf("My dir:%s\n",info->pw_dir);
snprintf(configPath,size,"%s/.config/systemd-gotify/config",info->pw_dir);
printf("ConfigPath:%s\n",configPath);
return 0;
}
2024-09-06 12:57:16 +00:00
int DBusOptions(DBusConnection *connection,DBusError **error) //not tested check pointer of error!!
{
int ret=0,i=0;
dbus_bool_t allow_user=0;
char **objects = NULL;
unsigned long uid;
ret = dbus_bus_request_name(connection,"test.method.client",DBUS_NAME_FLAG_REPLACE_EXISTING,*error);
if(dbus_error_is_set(*error))
2024-09-04 11:34:10 +00:00
{
2024-09-06 12:57:16 +00:00
fprintf(stderr,"%s",(**error).message);
dbus_error_free(*error);
2024-09-04 11:34:10 +00:00
return -1;
2024-09-12 10:15:43 +00:00
}else
2024-09-04 11:34:10 +00:00
{
printf("name requested from dbus: %d\n",ret);
}
2024-09-06 12:57:16 +00:00
2024-09-04 07:28:24 +00:00
dbus_connection_set_allow_anonymous(connection,allow_user);
printf("user auth dbus requ: %d\n",allow_user);
2024-09-04 11:34:10 +00:00
if(!dbus_connection_list_registered(connection,"/",&objects))
{
for(i=0;objects[i];i++)
{
printf("\nobject dbus: %s",objects[i]);
}
2024-09-12 10:15:43 +00:00
}else{
2024-09-04 11:34:10 +00:00
printf("dbus_connection_list_registered error\n");
}
2024-09-04 07:28:24 +00:00
if((ret=dbus_connection_get_unix_user(connection,&uid))==1)
{
printf("found USERID:%ld",uid);
2024-09-12 10:15:43 +00:00
}else
2024-09-04 07:28:24 +00:00
{
2024-09-06 11:44:12 +00:00
printf("NO userID set in dbus session, servers normali do not authenthicate against users/slaves: %d\n",ret);
2024-09-04 07:28:24 +00:00
}
2024-09-06 12:57:16 +00:00
return 0;
2024-09-04 07:28:24 +00:00
}
2024-09-08 15:22:28 +00:00
2024-09-06 12:57:16 +00:00
int GetSendStatus(DBusConnection *connection,char *objectpath)
{
char *result=0;
int ret = systemdUnitCall(connection,objectpath,"Get","SubState",&result);
2024-09-11 09:31:04 +00:00
if(ret == 0)
{
2024-09-16 08:33:55 +00:00
//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);
2024-09-19 07:26:07 +00:00
} else {
2024-09-11 10:02:23 +00:00
fprintf(stderr,"could not call systemdunit\n");
2024-09-11 09:31:04 +00:00
}
2024-09-06 12:57:16 +00:00
return ret;
}
2024-09-16 08:33:55 +00:00
int checkState(char *ServiceName,char *result,Service *service)
2024-09-06 11:44:12 +00:00
{
int i=0;
2024-09-06 12:57:16 +00:00
char *error_strings[]={"dead","inactive","exited"};
2024-09-19 07:26:07 +00:00
printf("result systemdUnitCall: %s\n",result);
2024-09-06 11:44:12 +00:00
for(i=0;i<(sizeof(error_strings)/sizeof(error_strings[0]));i++)
{
2024-09-11 13:21:20 +00:00
//printf("%s %ld\n",error_strings[i],sizeof(error_strings)/sizeof(error_strings[0]));
2024-09-16 08:33:55 +00:00
if(strcmp(result,error_strings[i])==0 && service->alerted == false)
2024-09-06 11:44:12 +00:00
{
2024-09-16 08:33:55 +00:00
service->alerted=true;
gotify_message(service->host,ServiceName,"is not running");
2024-09-06 11:44:12 +00:00
printf("!!system unit died!! Alert gotify server!\n");
}
2024-09-16 08:48:01 +00:00
else if(strcmp(result,"active")==0 || strcmp(result,"running")==0)
2024-09-06 11:44:12 +00:00
{
2024-09-16 08:33:55 +00:00
if(service->alerted == true)
{
gotify_message(service->host,ServiceName,"Running again, after crash");
2024-09-16 08:33:55 +00:00
}
service->alerted=false;
2024-09-19 07:26:07 +00:00
printf("service seems okay!!\n");
2024-09-16 08:33:55 +00:00
return 0;
2024-09-06 11:44:12 +00:00
}
}
return 1;
}
2024-09-04 07:28:24 +00:00
int gotify_message(char *url,char *title,char *message)
{
int ret;
CURL *curl;
2024-09-12 10:15:43 +00:00
CURLcode res,res2;
2024-09-04 07:28:24 +00:00
char post_buffer[128];
2024-09-11 13:21:20 +00:00
char url_buffer[128];
long response_code;
2024-09-04 07:28:24 +00:00
curl = curl_easy_init();
2024-09-06 12:57:16 +00:00
const char* token = NULL;
if(NULL==(token = getenv("GOTIFY_TOKEN")))
{
fprintf(stderr,"Pleas set a token using GOTIFY_TOKEN=\"abcde123\"\n");
}
else
{
2024-09-12 10:15:43 +00:00
printf("Got token from env\n");
2024-09-06 12:57:16 +00:00
ret = snprintf(url_buffer,sizeof(url_buffer),"%s/message?token=%s",url,token);
if(ret < sizeof(url_buffer))
{
2024-09-08 15:01:15 +00:00
printf("url:%s",url);
2024-09-06 12:57:16 +00:00
}
else
{
2024-09-11 10:02:23 +00:00
fprintf(stderr,"error connecting strings for url and token\n");
2024-09-06 12:57:16 +00:00
}
}
2024-09-06 11:44:12 +00:00
ret = snprintf(post_buffer,sizeof(post_buffer),"title=%s&message=%s&priority=6",title,message);
2024-09-04 07:28:24 +00:00
if(ret < sizeof(post_buffer))
{
2024-09-11 13:21:20 +00:00
printf("%s %d\n",post_buffer,ret);
2024-09-04 07:28:24 +00:00
if(curl)
{
2024-09-06 12:57:16 +00:00
curl_easy_setopt(curl,CURLOPT_URL,url_buffer);
2024-09-04 07:28:24 +00:00
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,post_buffer);
curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1L);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
{
2024-09-06 12:57:16 +00:00
fprintf(stderr,"curl_easy_perform failed: %s\n",curl_easy_strerror(res));
2024-09-04 07:28:24 +00:00
}
2024-09-12 10:15:43 +00:00
res2 = curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE,&response_code);
if((res2 == CURLE_OK) && response_code)
{
printf("Server returned:%ld\n",response_code);
}else
{
fprintf(stderr,"Error getting info; %ld\n",(long int)res2);
}
2024-09-04 07:28:24 +00:00
curl_easy_cleanup(curl);
2024-09-12 10:15:43 +00:00
}else
{
fprintf(stderr,"Error initializing curl context\n");
}
}else
{
2024-09-06 12:57:16 +00:00
printf("Error on string concelblalbla: %d\n",ret);
2024-09-04 07:28:24 +00:00
}
curl_easy_cleanup(curl);
2024-09-04 07:28:24 +00:00
return 0;
}
2024-09-04 12:52:19 +00:00
int systemdCall(DBusConnection *connection,char *method,char *param)
2024-09-04 11:34:10 +00:00
{
DBusError error;
DBusMessage *msg;
DBusMessage *ret_msg;
DBusMessageIter iter;
2024-09-04 12:52:19 +00:00
DBusMessageIter array_iter;
2024-09-04 11:34:10 +00:00
char *reply_msg=NULL;
2024-09-04 12:52:19 +00:00
int ret1,ret2;
2024-09-04 11:34:10 +00:00
dbus_error_init(&error);
2024-09-04 12:52:19 +00:00
msg = dbus_message_new_method_call("org.freedesktop.systemd1","/org/freedesktop/systemd1","org.freedesktop.systemd1.Manager",method);
2024-09-04 11:34:10 +00:00
if(NULL == msg)
{
2024-09-19 07:26:07 +00:00
fprintf(stderr,"Message null, no message created\n");
2024-09-04 11:34:10 +00:00
return 1;
}
else
{
printf("message created!\n");
}
2024-09-04 12:52:19 +00:00
if(param != NULL)
2024-09-04 11:34:10 +00:00
{
2024-09-04 12:52:19 +00:00
dbus_message_iter_init_append(msg,&iter);
if(!dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&param))
{
2024-09-19 07:26:07 +00:00
fprintf(stderr,"Error appending\n");
2024-09-04 12:52:19 +00:00
return -1;
}
2024-09-04 11:34:10 +00:00
}
2024-09-04 12:52:19 +00:00
2024-09-04 11:34:10 +00:00
ret_msg = dbus_connection_send_with_reply_and_block(connection,msg,DBUS_TIMEOUT_INFINITE,&error);
if(ret_msg == NULL)
{
fprintf(stderr,"error on receiving message from peer\n");
2024-09-04 11:34:10 +00:00
}
else
{
DBusMessageIter reply_iter;
dbus_message_iter_init(ret_msg,&reply_iter);
ret1=dbus_message_iter_get_arg_type(&reply_iter);
2024-09-04 12:52:19 +00:00
while(ret1 != DBUS_TYPE_INVALID)
2024-09-04 11:34:10 +00:00
{
2024-09-04 12:52:19 +00:00
if(ret1 == *DBUS_TYPE_OBJECT_PATH_AS_STRING)
{
printf("succesfull received message\narg type %c\n",ret1);
dbus_message_iter_get_basic(&reply_iter,&reply_msg);
printf("returned value: %s\n",reply_msg);
}
if(ret1 == DBUS_TYPE_STRING)
{
printf("succesfull received message\narg type %c\n",ret1);
dbus_message_iter_get_basic(&reply_iter,&reply_msg);
printf("Received message:%s\n",reply_msg);
}
if(ret1 == DBUS_TYPE_ARRAY)
{
int element_count = dbus_message_iter_get_element_count(&reply_iter);
printf("Array:Elem count %d\n",element_count);
//ret2 = dbus_message_iter_get_element_type(&reply_iter);
dbus_message_iter_recurse(&reply_iter,&array_iter);
int i2=0;
ret2 = dbus_message_iter_get_arg_type(&array_iter);
while(ret2 != DBUS_TYPE_INVALID)
{
DBusMessageIter sub_iter;
int ret3;
2024-09-19 07:26:07 +00:00
ret2 = dbus_message_iter_get_arg_type(&array_iter);
2024-09-04 12:52:19 +00:00
dbus_message_iter_recurse(&array_iter,&sub_iter);
ret3 = dbus_message_iter_get_arg_type(&sub_iter);
while(ret3 != DBUS_TYPE_INVALID)
{
ret3 = dbus_message_iter_get_arg_type(&sub_iter);
//printf("arr_sub_iter:%c\n",ret3);
if(ret3 == DBUS_TYPE_STRING)
{
dbus_message_iter_get_basic(&sub_iter,&reply_msg);
printf("%s\n",reply_msg);
2024-09-04 12:52:19 +00:00
}
ret3=dbus_message_iter_next(&sub_iter);
}
2024-09-19 07:26:07 +00:00
ret2=dbus_message_iter_next(&array_iter);
i2++;
2024-09-04 12:52:19 +00:00
}
}
ret1=dbus_message_iter_next(&reply_iter);
2024-09-04 11:34:10 +00:00
}
}
if(dbus_error_is_set(&error))
{
fprintf(stderr,"%s\n",error.message);
dbus_error_free(&error);
return -1;
}
return 0;
}
2024-09-05 12:48:23 +00:00
int systemdUnitCall(DBusConnection *connection,char *target,char *method,char *param,char **result)
{
DBusError error;
DBusMessage *msg;
DBusMessage *ret_msg;
DBusMessageIter iter;
DBusMessageIter array_iter;
DBusMessageIter variant_iter;
2024-09-06 11:44:12 +00:00
DBusMessageIter sub_iter;
int ret3;
char *reply_msg=NULL;
int ret1,ret2;
2024-09-05 12:48:23 +00:00
char *param2 = "org.freedesktop.systemd1.Unit";
dbus_error_init(&error);
//dest(servcice/bus) ,path(object) ,iface method
2024-09-05 12:48:23 +00:00
msg = dbus_message_new_method_call("org.freedesktop.systemd1",target,"org.freedesktop.DBus.Properties",method);
if(NULL == msg)
{
fprintf(stderr,"Message null, no message created\n");
dbus_message_unref(msg);
return 1;
}
2024-09-05 12:48:23 +00:00
if(param2 != NULL)
{
dbus_message_iter_init_append(msg,&iter);
2024-09-05 12:48:23 +00:00
if(!dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&param2))
{
2024-09-11 10:02:23 +00:00
fprintf(stderr,"Error appending\n");
dbus_message_unref(msg);
return -1;
}
2024-09-05 12:48:23 +00:00
if(!dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&param))
{
2024-09-11 10:02:23 +00:00
fprintf(stderr,"Error appending\n");
dbus_message_unref(msg);
return -1;
}
}
ret_msg = dbus_connection_send_with_reply_and_block(connection,msg,DBUS_TIMEOUT_INFINITE,&error);
if(ret_msg == NULL)
{
fprintf(stderr,"error ond receiving message from peer\n");
dbus_message_unref(msg);
dbus_message_unref(ret_msg);
2024-09-11 09:31:04 +00:00
return 1;
}
else
{
DBusMessageIter reply_iter;
dbus_message_iter_init(ret_msg,&reply_iter);
ret1=dbus_message_iter_get_arg_type(&reply_iter);
while(ret1 != DBUS_TYPE_INVALID)
{
2024-09-05 12:48:23 +00:00
if(ret1 == DBUS_TYPE_OBJECT_PATH)
{
printf("succesfull received message\narg type %c\n",ret1);
dbus_message_iter_get_basic(&reply_iter,&reply_msg);
printf("returned value: %s\n",reply_msg);
}
if(ret1 == DBUS_TYPE_STRING)
{
printf("succesfull received message\narg type %c\n",ret1);
2024-09-06 11:44:12 +00:00
printf("[func]reply_msg:%p , &reply_msg:%p , *reply_msg:%d\n", reply_msg,&reply_msg,*reply_msg);
dbus_message_iter_get_basic(&reply_iter,&reply_msg);
printf("Received message:%s\n",reply_msg);
}
if(ret1 == DBUS_TYPE_VARIANT)
{
dbus_message_iter_recurse(&reply_iter,&variant_iter);
char *type = dbus_message_iter_get_signature(&variant_iter);
2024-09-19 07:26:07 +00:00
//printf("str_val:%s\n",type);
if(type[0] == DBUS_TYPE_STRING)
{
2024-09-19 07:26:07 +00:00
//printf("[func]&result:%p , result:%p\n",&result,result);
2024-09-06 07:44:21 +00:00
dbus_message_iter_get_basic(&variant_iter,result);
2024-09-19 07:26:07 +00:00
//printf("*result:%s\n",*result);
}
dbus_free(type);
}
if(ret1 == DBUS_TYPE_ARRAY)
{
int element_count = dbus_message_iter_get_element_count(&reply_iter);
printf("Array:Elem count %d\n",element_count);
dbus_message_iter_recurse(&reply_iter,&array_iter);
ret2 = dbus_message_iter_get_arg_type(&array_iter);
while(ret2 != DBUS_TYPE_INVALID)
{
ret2 = dbus_message_iter_get_arg_type(&array_iter);
dbus_message_iter_recurse(&array_iter,&sub_iter);
ret3 = dbus_message_iter_get_arg_type(&sub_iter);
while(ret3 != DBUS_TYPE_INVALID)
{
ret3 = dbus_message_iter_get_arg_type(&sub_iter);
if(ret3 == DBUS_TYPE_STRING)
{
dbus_message_iter_get_basic(&sub_iter,&reply_msg);
2024-09-19 07:26:07 +00:00
//printf("%s\n",reply_msg);
}
ret3=dbus_message_iter_next(&sub_iter);
}
2024-09-06 11:44:12 +00:00
ret2=dbus_message_iter_next(&array_iter);
}
}
ret1=dbus_message_iter_next(&reply_iter);
}
}
if(dbus_error_is_set(&error))
{
fprintf(stderr,"%s\n",error.message);
dbus_error_free(&error);
dbus_message_unref(msg);
dbus_message_unref(ret_msg);
2024-09-11 09:31:04 +00:00
return 1;
2024-09-04 11:34:10 +00:00
}
dbus_message_unref(msg);
dbus_message_unref(ret_msg);
2024-09-04 11:34:10 +00:00
return 0;
}
2024-09-11 07:43:33 +00:00