Title: [opsview] [11292] Major speedup! Got a 90% speedup for a 12000 host system!
Revision
11292
Author
tvoon
Date
2013-01-31 01:33:17 +0000 (Thu, 31 Jan 2013)

Log Message

Major speedup! Got a 90% speedup for a 12000 host system!

Modified Paths


Modified: trunk/opsview-core/bin/nagconfgen.pl
===================================================================
--- trunk/opsview-core/bin/nagconfgen.pl	2013-01-30 17:29:13 UTC (rev 11291)
+++ trunk/opsview-core/bin/nagconfgen.pl	2013-01-31 01:33:17 UTC (rev 11292)
@@ -1484,39 +1484,39 @@
 
         # If there are multiple services associated, use that list
         if ( my $host_attributes_list =
-            $multiple_services_lookup->{ $row->{host}->name }
-            ->{ $row->{servicecheck}->name } )
+            $multiple_services_lookup->{ $row->{host}->{name} }
+            ->{ $row->{servicecheck}->{name} } )
         {
             $temp_service_list = $host_attributes_list;
         }
 
         # If it is meant to be a multiple services but nothing associated, then no dependency required
-        elsif ( $row->{servicecheck}->attribute ) {
+        elsif ( $row->{servicecheck}->{attribute} ) {
             $temp_service_list = [];
 
         }
 
         # Else this is a normal service check with dependency
         else {
-            $temp_service_list = [ $row->{servicecheck}->name ];
+            $temp_service_list = [ $row->{servicecheck}->{name} ];
         }
 
         foreach my $servicename (@$temp_service_list) {
-            next unless $hostname_lookup->{ $row->{host}->name };
+            next unless $hostname_lookup->{ $row->{host}->{name} };
 
             # Do some filtering to check that these services exist first, due to other logic
             # that may remove these definitions
             next
-              unless exists $host_services_lookup->{ $row->{host}->name }
-              ->{ $row->{dependency}->name };
+              unless exists $host_services_lookup->{ $row->{host}->{name} }
+              ->{ $row->{dependency}->{name} };
             next
-              unless exists $host_services_lookup->{ $row->{host}->name }
+              unless exists $host_services_lookup->{ $row->{host}->{name} }
               ->{$servicename};
 
             print OUTFILE "define servicedependency {
-host_name	" . $row->{host}->name . "
-service_description	" . $row->{dependency}->name . "
-dependent_host_name	" . $row->{host}->name . "
+host_name	" . $row->{host}->{name} . "
+service_description	" . $row->{dependency}->{name} . "
+dependent_host_name	" . $row->{host}->{name} . "
 dependent_service_description	" . $servicename . "
 notification_failure_criteria	w,c,u
 execution_failure_criteria w,c,u

Modified: trunk/opsview-core/lib/Opsview/Servicecheck.pm
===================================================================
--- trunk/opsview-core/lib/Opsview/Servicecheck.pm	2013-01-30 17:29:13 UTC (rev 11291)
+++ trunk/opsview-core/lib/Opsview/Servicecheck.pm	2013-01-31 01:33:17 UTC (rev 11292)
@@ -497,10 +497,10 @@
 Returns an arrayref with one row for each dependency that needs to be setup. Of format: 
 
   [
-    ( host => Opsview::Host, 
-      servicecheck => Opsview::Servicecheck, 
-      dependency => Opsview::Servicecheck,
-    ),
+    { host => { name => hostname },
+      servicecheck => { name => servicecheckname, attribute => attribute },
+      dependency => { name => dependencyservicecheckname },
+    },
     ...
   ]
 
@@ -520,7 +520,7 @@
     $dbh->do(
         qq{
 INSERT INTO temp_host_services_staging 
-SELECT hsc.hostid, hsc.servicecheckid 
+SELECT hsc.hostid, hsc.servicecheckid
 FROM hostservicechecks hsc, hosts h, monitoringservers ms
 WHERE hsc.hostid = h.id
 AND h.monitored_by = ms.id
@@ -533,7 +533,7 @@
     $dbh->do(
         qq{
 INSERT INTO temp_host_services_staging 
-SELECT hht.hostid, htsc.servicecheckid 
+SELECT hht.hostid, htsc.servicecheckid
 FROM hosthosttemplates hht 
 JOIN hosttemplateservicechecks htsc 
  ON hht.hosttemplateid = htsc.hosttemplateid 
@@ -548,64 +548,56 @@
     );
 
     # Now remove duplicates. Not sure if this is the right SQL, but works in 4.1
+    # We filter this by only services that are dependents
     $dbh->do(
         "CREATE TEMPORARY TABLE temp_host_services (hostid int, servicecheckid int)"
     );
     $dbh->do(
         "INSERT INTO temp_host_services
-SELECT DISTINCT(hostid), servicecheckid FROM temp_host_services_staging"
+SELECT DISTINCT(temp.hostid), temp.servicecheckid 
+FROM temp_host_services_staging temp, servicecheckdependencies scdep 
+WHERE temp.servicecheckid=scdep.dependencyid"
     );
-    $dbh->do( "DROP TEMPORARY TABLE temp_host_services_staging" );
 
-    # This create table is required because need to reference
-    # temp_host_services for query below. This is a work around to the fact
-    # that temporary tables cannot be referenced twice in a single SQL
-    # statement, even via MERGE engine
+    # Do the same thing, but this is for the services that have dependencies
     $dbh->do(
-        'CREATE TEMPORARY TABLE temp_host_services2 LIKE temp_host_services'
+        'CREATE TEMPORARY TABLE temp_host_services2 (hostid int, servicecheckid int, attribute int, servicecheckname VARCHAR(64))'
     );
-    $dbh->do( 'INSERT temp_host_services2 SELECT * FROM temp_host_services' );
+    $dbh->do(
+        'INSERT INTO temp_host_services2 
+SELECT DISTINCT(temp.hostid), temp.servicecheckid, sc.attribute, sc.name
+FROM temp_host_services_staging temp, servicechecks sc, servicecheckdependencies scdep 
+WHERE temp.servicecheckid=sc.id AND sc.id=scdep.servicecheckid'
+    );
 
-    my $sql;
-    my $sth;
-    if ( $ms->is_master ) {
-        $sql = <<"";
+    $dbh->do( "DROP TEMPORARY TABLE temp_host_services_staging" );
+
+    # The ORDER BY clause is for testing, but it is pretty fast so can leave it in
+    my $sql = <<"";
 SELECT
-hs.hostid as hostid, hs.servicecheckid as servicecheckid, scdep.dependencyid as dependencyid
+h.name as host_name, servicechecks.name as dependency_name, hs2.attribute as servicecheck_attribute, hs2.servicecheckname as servicecheck_name
 FROM
 temp_host_services hs,
 temp_host_services2 hs2,
 servicecheckdependencies scdep,
+hosts h,
 servicechecks
 WHERE
-hs.servicecheckid = scdep.servicecheckid
-and hs.hostid = hs2.hostid
-and hs2.servicecheckid = scdep.dependencyid
+hs2.servicecheckid = scdep.servicecheckid
+and hs2.hostid = hs.hostid
+and hs.servicecheckid = scdep.dependencyid
+and h.id = hs2.hostid
 and scdep.dependencyid = servicechecks.id
 and servicechecks.attribute IS NULL
+ORDER BY hs2.servicecheckid, hs2.hostid
 
+    my $sth;
+    if ( $ms->is_master ) {
         $sth = $dbh->prepare_cached($sql);
         $sth->execute;
     }
     else {
-        $sql = <<"";
-SELECT
-hs.hostid as hostid, hs.servicecheckid as servicecheckid, scdep.dependencyid as dependencyid
-FROM
-temp_host_services hs,
-temp_host_services2 hs2,
-servicecheckdependencies scdep,
-hosts h,
-servicechecks
-WHERE
-hs.servicecheckid = scdep.servicecheckid
-and hs.hostid = hs2.hostid
-and hs2.servicecheckid = scdep.dependencyid
-and h.id = hs.hostid
-and h.monitored_by = ?
-and scdep.dependencyid = servicechecks.id
-and servicechecks.attribute IS NULL
-
+        $sql .= " and h.monitored_by = ?";
         $sth = $dbh->prepare_cached($sql);
         $sth->execute( $ms->id );
     }
@@ -613,11 +605,12 @@
     my @results;
     while ( my $row = $sth->fetchrow_hashref ) {
         my $h = {};
-        $h->{host} = Opsview::Host->construct( { id => $row->{hostid} } );
-        $h->{servicecheck} =
-          Opsview::Servicecheck->construct( { id => $row->{servicecheckid} } );
-        $h->{dependency} =
-          Opsview::Servicecheck->construct( { id => $row->{dependencyid} } );
+        $h->{host} = { name => $row->{host_name} };
+        $h->{servicecheck} = {
+            name      => $row->{servicecheck_name},
+            attribute => $row->{servicecheck_attribute},
+        };
+        $h->{dependency} = { name => $row->{dependency_name} };
         push @results, $h;
     }
 

_______________________________________________
Opsview-checkins mailing list
[email protected]
http://lists.opsview.org/lists/listinfo/opsview-checkins

Reply via email to