yasnd/yasnd.c

304 lines
6.8 KiB
C
Raw Normal View History

2011-11-15 14:41:46 +04:00
#include <sys/types.h>
#include <sys/stat.h>
2011-12-13 19:09:17 +04:00
#include <sys/io.h>
2011-11-15 17:53:12 +04:00
#include <sys/wait.h>
2011-11-15 14:41:46 +04:00
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
2011-11-15 17:53:12 +04:00
#include <signal.h>
2011-11-17 15:45:44 +04:00
#include <confuse.h>
2011-11-15 14:41:46 +04:00
#include "yasnd.h"
2011-11-15 17:53:12 +04:00
2011-11-25 17:50:47 +04:00
cfg_t *cfg; // pointer to configuration structure
2011-11-25 17:35:33 +04:00
host_decl* hosts=NULL; // structure with hosts' definitions
int hosts_count=0; // count of hosts
int debug_flag=0;
2011-12-12 13:47:43 +04:00
bool sms_send_enable=false; // send or not send alerts via SMS
char* phone_device=NULL; // phone device for gammu, for example: "/dev/ttyACM0"
char* phone_model=NULL; // gammu phone model, for example: "at"
char* phone_connection=NULL; // gammu phone connection type, for example: "at"
char* recipient_number=NULL; // recipient of sms alerts
bool loop_locked=false ; // flag for locking main loop while config re-reading
2011-11-15 17:53:12 +04:00
2011-11-25 17:35:33 +04:00
void log_debug(const char *message,int verbosity)
2011-11-15 17:53:12 +04:00
{
2011-11-25 17:35:33 +04:00
if (debug_flag>=verbosity)
2011-11-15 17:53:12 +04:00
syslog(LOG_INFO,"%s",message);
2011-11-25 17:35:33 +04:00
}
void log_event(const char *message)
{
syslog(LOG_INFO,"%s",message);
2011-11-15 17:53:12 +04:00
}
2011-11-15 14:41:46 +04:00
void* allocate_memory(int bytes)
{
void* result=malloc(bytes);
if (result==NULL)
{
log_event("Error: malloc failed");
exit(EXIT_FAILURE);
}
return result;
}
void init()
{
// Initialize config parsing library
cfg_opt_t opts[] = {
CFG_STR_LIST("hosts", "{}", CFGF_NONE),
2011-11-25 17:35:33 +04:00
CFG_SIMPLE_INT("debug", &debug_flag),
2011-12-12 13:47:43 +04:00
CFG_SIMPLE_BOOL("sms_send_enable", &sms_send_enable),
CFG_SIMPLE_STR("phone_device", &phone_device),
CFG_SIMPLE_STR("phone_model", &phone_connection),
CFG_SIMPLE_STR("phone_connection", &phone_model),
CFG_SIMPLE_STR("sms_recipient", &recipient_number),
CFG_END()
};
cfg = cfg_init(opts, CFGF_NONE);
// check if config file is valid and parse it
if (cfg_parse(cfg, "/etc/yasnd.conf") == CFG_PARSE_ERROR)
{
2011-11-25 17:35:33 +04:00
log_event("Error parsing config file");
exit(EXIT_FAILURE);
}
// if SMS sending is enabled, all of phone parameters must be explicitly defined
2011-12-12 13:47:43 +04:00
if (sms_send_enable)
{
if (phone_model==NULL)
{
log_event("Error: phone model is not defined in config file");
exit(EXIT_FAILURE);
}
if (phone_connection==NULL)
{
log_event("Error: phone connection type is not defined in config file");
exit(EXIT_FAILURE);
}
if (phone_device==NULL)
{
log_event("Error: phone device is not defined in config file");
exit(EXIT_FAILURE);
}
2011-12-12 13:47:43 +04:00
if (recipient_number==NULL)
{
log_event("Error: no recipient for sms alerts defined in config file");
exit(EXIT_FAILURE);
}
}
2011-11-25 17:42:48 +04:00
// hosts' array must not be empty
2011-11-25 17:35:33 +04:00
hosts_count=cfg_size(cfg,"hosts");
2011-11-25 17:42:48 +04:00
if (hosts_count==0)
{
log_event("Error: no hosts to check defined in config file");
exit(EXIT_FAILURE);
}
// allocate memory for hosts array...
2011-11-25 17:35:33 +04:00
hosts=allocate_memory(hosts_count*sizeof(host_decl));
// ...and fill it with config file content
2011-11-25 17:35:33 +04:00
for (int i=0;i<hosts_count;i++)
{
hosts[i].hostname=cfg_getnstr(cfg,"hosts",i);
hosts[i].fail_count=0;
hosts[i].alert_sent=false;
}
2011-11-25 18:30:16 +04:00
// initialize gammu structures
2011-11-30 16:21:34 +04:00
gammu_init();
}
void check_host(int num,host_decl* host)
2011-11-15 17:53:12 +04:00
{
pid_t pid = fork();
if (pid < 0) {
2011-11-25 17:35:33 +04:00
log_event("Error: can not fork child :(");
2011-11-15 14:41:46 +04:00
exit(EXIT_FAILURE);
}
2011-11-15 17:53:12 +04:00
// If pid == 0, then we are in the child
if (pid == 0) {
char tmp[50];
sprintf(tmp, "Pinging host %s", host->hostname);
2011-11-25 17:35:33 +04:00
log_debug(tmp,DEBUG_BASE);
2011-11-30 16:21:34 +04:00
execl("/bin/ping","/bin/ping","-c 1","-n",host->hostname,(char*) 0);
2011-11-15 17:53:12 +04:00
// STUB: check result of exec call
exit(EXIT_SUCCESS);
2011-11-15 14:41:46 +04:00
}
2011-11-15 17:53:12 +04:00
// Save child's pid
host->helper_pid=pid;
2011-11-15 17:53:12 +04:00
}
2011-12-13 19:09:17 +04:00
/*
Function, that turn on only one LPT port pin, defined by it argument, wait a second,
and turn off all pins. That is enough to reset specified host.
*/
void reset_pin(int pin_num)
{
if (pin_num<1 || pin_num>8)
{
log_event("Error: incorrent LPT pin number");
return;
}
#define LPTPORT 0x378
int pins = 2<<(pin_num-1);
if (ioperm (LPTPORT, 3, 1))
{
log_event("Error: LPT port access error");
exit(EXIT_FAILURE);
}
// Reset host
outb (pins, LPTPORT);
sleep(1);
outb (0, LPTPORT);
}
2011-11-15 17:53:12 +04:00
void loop_function()
{
2011-11-25 17:35:33 +04:00
for (int i=0;i<hosts_count;i++)
2011-11-15 17:53:12 +04:00
{
check_host(i,&hosts[i]);
2011-11-15 17:53:12 +04:00
}
2011-11-25 17:35:33 +04:00
for (int i=0;i<hosts_count;i++)
2011-11-15 17:53:12 +04:00
{
int pid_status;
2011-11-25 17:35:33 +04:00
int result = waitpid(hosts[i].helper_pid, &pid_status, 0);
2011-11-15 17:53:12 +04:00
if (result > 0)
{
if (WIFEXITED(pid_status))
{
char tmp[50];
sprintf(tmp, "Finished child %d with result %d", result, WEXITSTATUS(pid_status));
2011-11-25 17:35:33 +04:00
log_debug(tmp,DEBUG_ALL);
2011-11-15 17:53:12 +04:00
// if host is alive - reset failure counter
// otherwise - increment it
if (WEXITSTATUS(pid_status) == 0)
{
2011-11-15 17:53:12 +04:00
hosts[i].fail_count=0;
hosts[i].alert_sent=false;
}
2011-11-15 17:53:12 +04:00
else
hosts[i].fail_count++;
}
}
}
2011-11-30 16:21:34 +04:00
for (int i=0;i<hosts_count;i++)
{
if (hosts[i].fail_count>0 && !hosts[i].alert_sent)
2011-11-30 16:21:34 +04:00
{
char message[100];
sprintf(message,"Host %s does not answer",hosts[i].hostname);
log_debug(message,DEBUG_BASE);
2011-12-12 13:47:43 +04:00
if (sms_send_enable)
gammu_send_sms(message);
// set alert flag to prevent sending more than 1 message
// for unreachable host
hosts[i].alert_sent=true;
2011-11-30 16:21:34 +04:00
}
}
2011-11-15 17:53:12 +04:00
}
void signal_handler(int signum)
2011-11-15 17:53:12 +04:00
{
void free_resources()
{
// free config structure
cfg_free(cfg);
// free gammu structure
GSM_FreeStateMachine(state_machine);
// free hosts structures memory
if (hosts!=NULL)
free(hosts);
}
if (signum==SIGHUP)
{
log_event("SIGHUP captured, daemon re-read config");
// lock Main Loop
loop_locked=true;
// STUB: need to kill all childs here
// free all resources
free_resources();
// run init again
init();
// sleep 5 seconds and unlock Main Loop
sleep(5);
loop_locked=false;
return;
}
if (signum==SIGTERM)
{
log_event("SIGTERM captured, daemon stopping");
closelog();
free_resources(); // free all other resources
exit(EXIT_SUCCESS);
}
2011-11-15 17:53:12 +04:00
}
int main(void) {
/* Our process ID and Session ID */
pid_t pid, sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
/* If we got a good PID, then we can exit the parent process. */
if (pid > 0) {
exit(EXIT_SUCCESS);
}
/* Change the file mode mask */
umask(0);
/* Open any logs here */
openlog("yasnd", LOG_PID|LOG_CONS, LOG_USER);
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) {
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Change the current working directory */
if ((chdir("/")) < 0) {
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
// Set signal handlers
signal(SIGTERM, signal_handler);
signal(SIGHUP, signal_handler);
2011-11-15 17:53:12 +04:00
// Init apropriate structures
init();
/* The Main Loop */
2011-11-15 17:53:12 +04:00
while (1) {
if (!loop_locked)
{
loop_function();
// sleep(60); /* wait 60 seconds */
log_event("Exiting");
exit(EXIT_SUCCESS);
}
else
{
// if loop is locked, just wait 1 second and start it again
sleep(1);
}
2011-11-15 17:53:12 +04:00
}
exit(EXIT_SUCCESS);
2011-11-15 14:41:46 +04:00
}