[icinga-checkins] icinga.org: icinga-core/hbaecker/perl-tests: fix status.dat ok, no daemon running, show error instead of old data

git at icinga.org git at icinga.org
Sun Jan 17 15:47:56 CET 2010


Module: icinga-core
Branch: hbaecker/perl-tests
Commit: a2086681a09821c08bd9bc4c995a9ac749b33dd2
URL:    https://git.icinga.org/?p=icinga-core.git;a=commit;h=a2086681a09821c08bd9bc4c995a9ac749b33dd2

Author: Michael Friedrich <michael.friedrich at univie.ac.at>
Date:   Sun Jan 10 00:30:29 2010 +0100

fix status.dat ok, no daemon running, show error instead of old data

1/ lockfile exists, if no, check if program name in ps -C icinga -o pid
2/ program name is running, if no bail out
3/ if lockfile exists, then reads pid from lockfile and checks if pid can
 be found in running processes using ps -o pid -p $pid
4/ if no pid found, lock file might be broken

---

 cgi/cgiutils.c   |  103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/common.h |    1 +
 2 files changed, 104 insertions(+), 0 deletions(-)

diff --git a/cgi/cgiutils.c b/cgi/cgiutils.c
index 1057566..74c0ea5 100644
--- a/cgi/cgiutils.c
+++ b/cgi/cgiutils.c
@@ -563,6 +563,11 @@ int read_all_status_data(char *config_file,int options){
 	if(service_status_has_been_read==TRUE && (options & READ_SERVICE_STATUS))
 		options-=READ_SERVICE_STATUS;
 
+        /* return error if daemon is not running */
+        if(check_daemon_running()==ERROR) {
+                return ERROR;
+        }
+
 	/* bail out if we've already read what we need */
 	if(options<=0)
 		return OK;
@@ -2271,3 +2276,101 @@ void strip_splunk_query_terms(char *buffer){
 	}
 
 
+/**********************************************************
+*************** CHECK CORE FUNCTIONS **********************
+**********************************************************/
+
+/* code partly taken from contrib/daemonchk.c */
+
+/* checks if core daemon is running for showing live data or an error */
+int check_daemon_running(void) {
+
+#define CHARLEN 256
+
+	char *lock_file=NULL;
+	char *proc_file=NULL;
+	char *input = NULL;
+	char *val = NULL;
+	char *daemon_name = NULL;
+	struct stat statbuf;
+	int pid, testpid, found;
+	char input_buffer[CHARLEN];
+	mmapfile *fk;
+	FILE *fp;
+
+	/* find lock file. get pid if it exists */
+	if(asprintf(&lock_file,"%s",DEFAULT_LOCK_FILE)==-1) {
+                return ERROR;
+        }
+
+	if((fk=mmap_fopen(lock_file))==NULL) {
+
+		/* no lock file found try program name instead */
+                asprintf(&daemon_name,"%s",PROGRAM_NAME_LC);
+
+                if(asprintf(&proc_file,"/bin/ps -o pid -C %s",daemon_name)==-1) {
+                        free(proc_file);
+                        return ERROR;
+                }
+
+                if((fp=popen(proc_file, "r"))==NULL) {
+                        free(proc_file);
+                        return ERROR;
+                }
+
+                fgets(input_buffer,CHARLEN-1,fp);
+                fgets(input_buffer,CHARLEN-1,fp);
+
+                /* check if entry found */
+                if (sscanf(input_buffer,"%d",&testpid)!=1) {
+                        free(proc_file);
+                        return ERROR;
+                } else {
+			/* daemon is running */
+			free(proc_file);
+			return OK;
+		}
+	}
+        
+	if((input=mmap_fgets(fk))==NULL) {
+		mmap_fclose(fk);
+		free(lock_file);
+		return ERROR;
+	}
+
+	strip(input);
+        val=strtok(input,"\n");
+	pid=atoi(val);
+
+        free(input);
+        mmap_fclose(fk);
+	free(lock_file);
+
+	/* find pid in running processes to check if core died without removing lock file */
+	free(proc_file);
+	if(asprintf(&proc_file,"/bin/ps -o pid -p %d",pid)==-1) {
+		free(lock_file);
+		return ERROR;
+	}
+
+        if((fp=popen(proc_file, "r"))==NULL) {
+		free(proc_file);
+                return ERROR;
+        }
+
+	fgets(input_buffer,CHARLEN-1,fp);
+	fgets(input_buffer,CHARLEN-1,fp);
+
+	/* check if correct pid found */
+	if (sscanf(input_buffer,"%d",&testpid)==1) {
+		if (testpid!=pid) {
+	                free(proc_file);
+			return ERROR;
+		}
+	} 
+
+	free(proc_file);	
+
+	return OK;
+}
+
diff --git a/include/common.h b/include/common.h
index e9027e2..85cd995 100644
--- a/include/common.h
+++ b/include/common.h
@@ -25,6 +25,7 @@
 
 #define PROGRAM_NAME "Icinga"
 #define PROGRAM_NAME_UC "ICINGA"
+#define PROGRAM_NAME_LC "icinga"
 #define PROGRAM_VERSION "1.0"
 #define PROGRAM_MODIFICATION_DATE "12-16-2009"
 





More information about the icinga-checkins mailing list