CGI speedup (patch against 1.1)

David Parrish david at dparrish.com
Mon Aug 4 05:09:51 CEST 2003


I'm currently running a nagios instance which is monitoring almost 7000
services, 95% of which are passive. I'd found that the load on the single
server is generally quite high. The patch that Michael posted earlier made
a huge difference to the general load on the system, but the CGI scripts
(status.cgi) still takes around 13-14 seconds per view. 

Profiling the CGI revealed that roughly 90% of the cpu time is spent in
find_service, because it does a linear walk along the linked list of all
the services, and it does it sometimes 4 times per service because of the
amount of processing required to display a nice pretty sorted list.

My quick fix (not a perfect solution) was to build a hash of the service &
host names and use that to very quickly find the record in RAM. I've used
glib because it has very simple hashing routines, but any hashing code
would probably do fine.

After applying the patch, the CGI takes around 4 seconds per view, which is
a huge improvement.

However, using the nagios core compiled with this code appears to run a lot
hotter, and makes the load skyrocket to around 60 or so... So I just use
this patch with the CGI.

Patch against release 1.1 is attached...

-- 
Regards,
David Parrish
0410 586 121
-------------- next part --------------
diff -u -b -B -r1.1.1.1 Makefile.in
--- base/Makefile.in	23 Jun 2003 01:56:10 -0000	1.1.1.1
+++ base/Makefile.in	30 Jul 2003 01:22:20 -0000
@@ -10,9 +10,9 @@
 SRC_XDATA=../xdata
 
 CC=@CC@
-CFLAGS=@CFLAGS@ @DEFS@ -DNSCORE
+CFLAGS=@CFLAGS@ @DEFS@ -DNSCORE `glib-config --cflags`
 #CFLAGS=-O3 -Wall -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -DHAVE_CONFIG_H -DNSCORE
-LDFLAGS=@LDFLAGS@ @LIBS@
+LDFLAGS=@LDFLAGS@ @LIBS@ `glib-config --libs`
 
 prefix=@prefix@
 exec_prefix=@exec_prefix@
diff -u -b -B -r1.1.1.1 Makefile.in
--- cgi/Makefile.in	23 Jun 2003 01:56:10 -0000	1.1.1.1
+++ cgi/Makefile.in	30 Jul 2003 01:22:20 -0000
@@ -25,9 +25,9 @@
 
 CP=@CP@
 CC=@CC@
-CFLAGS=@CFLAGS@ @DEFS@ -DNSCGI
+CFLAGS=@CFLAGS@ @DEFS@ -DNSCGI `glib-config --cflags`
 #CFLAGS=-O3 -Wall -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -DHAVE_CONFIG_H -DNSCGI
-LDFLAGS=@LDFLAGS@ @LIBS@
+LDFLAGS=@LDFLAGS@ @LIBS@ `glib-config --libs`
 
 CGIS=avail.cgi cmd.cgi config.cgi extinfo.cgi history.cgi notifications.cgi outages.cgi showlog.cgi status.cgi statuswml.cgi summary.cgi tac.cgi $(CGIEXTRAS)
 
diff -u -b -B -r1.1.1.2 common.h
--- common/common.h	23 Jun 2003 01:57:24 -0000	1.1.1.2
+++ common/common.h	30 Jul 2003 01:22:20 -0000
@@ -208,8 +208,12 @@
 #define	OK				0
 #define ERROR				-2	/* value was changed from -1 so as to not interfere with STATUS_UNKNOWN plugin result */
 
+#ifndef TRUE
 #define TRUE				1
+#endif
+#ifndef FALSE
 #define FALSE				0
+#endif
 
 
 /****************** HOST CONFIG FILE READING OPTIONS ********************/
diff -u -b -B -r1.1.1.1 config.h.in
--- common/config.h.in	23 Jun 2003 01:56:10 -0000	1.1.1.1
+++ common/config.h.in	30 Jul 2003 01:22:20 -0000
@@ -214,3 +214,7 @@
 #include <dirent.h>
 #endif
 
+#define HAVE_GLIB_H 1
+#ifdef HAVE_GLIB_H
+#include <glib.h>
+#endif
diff -u -b -B -r1.1.1.1 objects.c
--- common/objects.c	23 Jun 2003 01:56:10 -0000	1.1.1.1
+++ common/objects.c	30 Jul 2003 01:22:20 -0000
@@ -59,6 +59,8 @@
 servicedependency       *servicedependency_list=NULL;
 hostdependency          *hostdependency_list=NULL;
 hostescalation          *hostescalation_list=NULL;
+GHashTable	*service_hash = NULL;
+GHashTable	*host_hash = NULL;
 
 
 
@@ -747,6 +749,16 @@
 	printf("\tNotification Interval:    %d\n",new_host->notification_interval);
 	printf("\tNotification Time Period: %s\n",new_host->notification_period);
 #endif
+
+#ifdef HAVE_GLIB_H
+	/* Cache host in hash table */
+	if (!host_hash)
+		host_hash = g_hash_table_new(g_str_hash, g_str_equal);
+
+	if (host_hash)
+		g_hash_table_insert(host_hash, new_host->name, new_host);
+#endif
+
 #ifdef DEBUG0
 	printf("add_host() end\n");
 #endif
@@ -2222,6 +2234,19 @@
 	printf("\tEvent Handler:            %s\n",(new_service->event_handler==NULL)?"N/A":new_service->event_handler);
 #endif
 
+#ifdef HAVE_GLIB_H
+	/* Cache service in hash table */
+	if (!service_hash)
+		service_hash = g_hash_table_new(g_str_hash, g_str_equal);
+
+	if (service_hash)
+	{
+		char *key = calloc(strlen(new_service->host_name) + strlen(new_service->description) + 2, 1);
+		sprintf(key, "%s-%s", new_service->host_name, new_service->description);
+		g_hash_table_insert(service_hash, key, new_service);
+	}
+#endif
+
 #ifdef DEBUG0
 	printf("add_service() end\n");
 #endif
@@ -3226,6 +3251,13 @@
 	if(name==NULL)
 		return NULL;
 
+#ifdef HAVE_GLIB_H
+	/* Lookup host in the hash */
+	if (host_hash)
+		return (host *)g_hash_table_lookup(host_hash, name);
+#endif
+
+
 	if(hst==NULL)
 		temp_host=host_list;
 	else
@@ -3500,6 +3532,18 @@
 	if(host_name==NULL || svc_desc==NULL)
 		return NULL;
 
+#ifdef HAVE_GLIB_H
+	/* Lookup service in the hash */
+	if (service_hash)
+	{
+		char *key = calloc(strlen(host_name) + strlen(svc_desc) + 2, 1);
+		sprintf(key, "%s-%s", host_name, svc_desc);
+		temp_service = (service *)g_hash_table_lookup(service_hash, key);
+		free(key);
+		return temp_service;
+	}
+#endif
+
 	if(svcptr==NULL)
 		temp_service=service_list;
 	else
diff -u -b -B -r1.1.1.2 Makefile.in
--- contrib/Makefile.in	23 Jun 2003 01:57:24 -0000	1.1.1.2
+++ contrib/Makefile.in	30 Jul 2003 01:22:20 -0000
@@ -5,8 +5,8 @@
 ###############################
 
 CC=@CC@
-CFLAGS=@CFLAGS@ @DEFS@
-LDFLAGS=@LDFLAGS@ @LIBS@
+CFLAGS=@CFLAGS@ @DEFS@ `glib-config --cflags`
+LDFLAGS=@LDFLAGS@ @LIBS@ `glib-config --libs`
 
 # Source code directories
 SRC_COMMON=../common
diff -u -b -B -r1.1.1.1 Makefile.in
--- html/Makefile.in	23 Jun 2003 01:56:10 -0000	1.1.1.1
+++ html/Makefile.in	30 Jul 2003 01:22:20 -0000
@@ -1,6 +1,6 @@
 CC=@CC@
-CFLAGS=@CFLAGS@ @DEFS@
-LDFLAGS=@LDFLAGS@ @LIBS@
+CFLAGS=@CFLAGS@ @DEFS@ `glib-config --cflags`
+LDFLAGS=@LDFLAGS@ @LIBS@ `glib-config --libs`
 
 prefix=@prefix@
 exec_prefix=@exec_prefix@
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <https://www.monitoring-lists.org/archive/developers/attachments/20030804/4ba9d6dc/attachment.sig>


More information about the Developers mailing list