Title: [opsview] [9586] exclude handled services/hosts from viewports if requested
Revision
9586
Author
aburzynski
Date
2012-07-18 15:36:44 +0100 (Wed, 18 Jul 2012)

Log Message

exclude handled services/hosts from viewports if requested

Modified Paths


Modified: trunk/opsview-core/lib/Runtime/ResultSet/OpsviewViewports.pm
===================================================================
--- trunk/opsview-core/lib/Runtime/ResultSet/OpsviewViewports.pm	2012-07-18 14:12:47 UTC (rev 9585)
+++ trunk/opsview-core/lib/Runtime/ResultSet/OpsviewViewports.pm	2012-07-18 14:36:44 UTC (rev 9586)
@@ -65,12 +65,14 @@
             "me.keyword AS keyword_name",
             "me.host_object_id",
             \"count(*)",
+            "opsview_keyword.exclude_handled",
             ],
         "+as" => [
             "description",
             "keyword_name",
             "host_object_id",
             "total",
+            "exclude_handled",
             ],
         group_by => ["me.keyword", "me.host_object_id", "servicestatus.current_state", "service_unhandled"],
         order_by => ["me.keyword", "me.host_object_id"],

Modified: trunk/opsview-core/lib/Runtime/ResultSet.pm
===================================================================
--- trunk/opsview-core/lib/Runtime/ResultSet.pm	2012-07-18 14:12:47 UTC (rev 9585)
+++ trunk/opsview-core/lib/Runtime/ResultSet.pm	2012-07-18 14:36:44 UTC (rev 9586)
@@ -25,7 +25,8 @@
 use base qw/DBIx::Class::ResultSet/;
 
 use Opsview::Utils
-  qw(convert_to_arrayref convert_host_state_to_text convert_state_to_text set_highest_state);
+  qw(convert_to_arrayref convert_host_state_to_text convert_state_to_text
+  set_highest_state max_state);
 
 my $state_type_lookup = {
     "soft" => 0,
@@ -218,6 +219,9 @@
     my $last_group_key   = "";
     my $last_hash;
     my $found_hosts;
+    my $highest_host_unhandled_state;
+    my $highest_service_unhandled_state;
+    my $uses_exclude_handled = 0;
     my $status;
     my $summary = {
         host => {
@@ -263,7 +267,22 @@
         #$status->{summary}->{handled}   = $status->{hosts}->{handled} + $status->{services}->{handled};
         #$status->{summary}->{unhandled} = $status->{hosts}->{unhandled} + $status->{services}->{unhandled};
         #$status->{summary}->{total}     = $status->{summary}->{handled} + $status->{summary}->{unhandled};
-        set_highest_state($status);
+
+        if ( $args->{viewport_check_exclude_handled} && $uses_exclude_handled )
+        {
+            $status->{computed_state} = convert_state_to_text(
+                max_state(
+                    $highest_host_unhandled_state,
+                    $highest_service_unhandled_state
+                )
+            );
+            $status->{services}->{computed_state} =
+              convert_state_to_text($highest_service_unhandled_state);
+        }
+        else {
+            set_highest_state($status);
+        }
+
         push @list, $status;
     };
     while ( my $hash = $self->next ) {
@@ -288,8 +307,32 @@
             $last_group_label = $hash->{$group_label};
             $last_group_key   = $hash->{$group_key};
             $last_hash        = $hash;
+            undef $highest_host_unhandled_state;
+            undef $highest_service_unhandled_state;
+            $uses_exclude_handled = 0;
         }
 
+        unless ( defined $highest_host_unhandled_state ) {
+            $highest_host_unhandled_state = $hash->{host_state};
+        }
+        unless ( defined $highest_service_unhandled_state ) {
+            $highest_service_unhandled_state = $hash->{service_state};
+        }
+
+        $uses_exclude_handled = $hash->{exclude_handled};
+
+        if ( $args->{viewport_check_exclude_handled} && $uses_exclude_handled )
+        {
+            $highest_host_unhandled_state =
+              max_host_unhandled_state( $highest_host_unhandled_state, $hash )
+              if $hash->{host_unhandled};
+
+            $highest_service_unhandled_state =
+              max_service_unhandled_state( $highest_service_unhandled_state,
+                $hash )
+              if $hash->{service_unhandled};
+        }
+
         my $key;
         if ( !exists $found_hosts->{ $hash->{host_object_id} } ) {
             $found_hosts->{ $hash->{host_object_id} }++;
@@ -343,6 +386,22 @@
     };
 }
 
+sub max_host_unhandled_state {
+    my ( $current_max, $hash ) = @_;
+
+    # host is not ok - set to critical
+    return max_state( $current_max, 2 ) if $hash->{host_state} > 0;
+
+    return max_state( $current_max, $hash->{host_state} );
+}
+
+sub max_service_unhandled_state {
+    my ( $current_max, $hash ) = @_;
+
+    # return new max
+    return max_state( $current_max, $hash->{service_state} );
+}
+
 # We run downtimes separately because there could be lots of downtime for each
 # object, which makes other queries grow exponentially
 # We group results based on the incoming key

Modified: trunk/opsview-web/lib/Opsview/Web/Controller/REST/Status/Viewport.pm
===================================================================
--- trunk/opsview-web/lib/Opsview/Web/Controller/REST/Status/Viewport.pm	2012-07-18 14:12:47 UTC (rev 9585)
+++ trunk/opsview-web/lib/Opsview/Web/Controller/REST/Status/Viewport.pm	2012-07-18 14:36:44 UTC (rev 9586)
@@ -112,7 +112,13 @@
 
     my $rs     = $c->stash->{rs};
     my $args   = $c->stash->{status_params} || { %{ $c->req->params } };
-    my $result = $rs->list_summary($args);
+    my $result = $rs->list_summary(
+        $args,
+        {
+            viewport_check_exclude_handled =>
+              delete $c->stash->{viewport_check_exclude_handled}
+        }
+    );
 
     $c->stash( 'rest' => $result );
 }

Modified: trunk/opsview-web/lib/Opsview/Web/Controller/Viewport.pm
===================================================================
--- trunk/opsview-web/lib/Opsview/Web/Controller/Viewport.pm	2012-07-18 14:12:47 UTC (rev 9585)
+++ trunk/opsview-web/lib/Opsview/Web/Controller/Viewport.pm	2012-07-18 14:36:44 UTC (rev 9586)
@@ -115,7 +115,8 @@
     }
 
     my $params = { %{ $c->req->params } };
-    $c->stash( status_params => $params );
+    $c->stash( status_params                  => $params );
+    $c->stash( viewport_check_exclude_handled => 1 );
     $c->forward( "/rest/status/viewport/viewport_GET" );
     $c->stash( status_data  => $c->stash->{rest} );
     $c->stash( heading_list => [ $c->loc("ui.viewport.summary.title") ] );

Modified: trunk/opsview-web/t/840viewports.t
===================================================================
--- trunk/opsview-web/t/840viewports.t	2012-07-18 14:12:47 UTC (rev 9585)
+++ trunk/opsview-web/t/840viewports.t	2012-07-18 14:36:44 UTC (rev 9586)
@@ -14,6 +14,7 @@
 use Sys::Hostname;
 use Data::Dump qw(dump);
 use Opsview::Utils;
+use JSON;
 
 use Opsview::Web::Test;
 use Opsview::TestUtils;
@@ -21,7 +22,7 @@
 use Opsview;
 use Opsview::Contact;
 
-use Test::More qw(no_plan);
+use Test::More;
 use Test::Deep;
 
 use Opsview::Schema;
@@ -207,10 +208,81 @@
     "http://$hostname/viewport?output=json&keyword=cisco&keyword=cisco_gp1&keyword=cisco_gp2"
 );
 ok( $res->is_success, "Got viewport summary page in json output" );
-use JSON;
 my $obj = from_json( $res->content );
 is_deeply( $obj, $expected, "Object as expected" ) || diag explain $obj;
 
+# Exclude handled for "allhandled"
+$admin->get_ok( "/admin/keyword/edit/9" );
+$admin->form_name( "main_form" );
+$admin->field( "exclude_handled", 1 );
+$admin->submit_form;
+
+# Check public viewports
+$expected = {
+    'ResultSet' => {
+        'list' => [
+            {
+                'computed_state' => 'ok',
+                'description'    => 'All services handled',
+                'downtime'       => undef,
+                'hosts'          => {
+                    'down'      => { 'handled' => 1 },
+                    'handled'   => 1,
+                    'total'     => 1,
+                    'unhandled' => 0
+                },
+                'name'     => 'allhandled',
+                'services' => {
+                    'computed_state' => 'ok',
+                    'handled'        => 1,
+                    'total'          => 1,
+                    'unhandled'      => 0,
+                    'unknown'        => { 'handled' => 1 }
+                }
+            }
+        ],
+        'summary' => {
+            'handled' => 2,
+            'host'    => {
+                'down'      => 1,
+                'handled'   => 1,
+                'total'     => 1,
+                'unhandled' => 0
+            },
+            'service' => {
+                'handled'   => 1,
+                'total'     => 1,
+                'unhandled' => 0,
+                'unknown'   => 1
+            },
+            'total'     => 2,
+            'unhandled' => 0
+        }
+    }
+};
+
+$res =
+  $public->get( "http://$hostname/viewport?output=json&keyword=allhandled" );
+ok( $res->is_success, "Got viewport summary page in json output" );
+$obj = from_json( $res->content );
+is_deeply( $obj, $expected, "Object as expected" ) || diag explain $obj;
+
+# Reset Exclude handled for "cisco"
+$admin->get_ok( "/admin/keyword/edit/9" );
+$admin->form_name( "main_form" );
+$admin->field( "exclude_handled", 0 );
+$admin->submit_form;
+
+# Reset computed_state
+$expected->{ResultSet}{list}[0]{computed_state} = 'critical';
+$expected->{ResultSet}{list}[0]{services}{computed_state} = 'unknown';
+
+$res =
+  $public->get( "http://$hostname/viewport?output=json&keyword=allhandled" );
+ok( $res->is_success, "Got viewport summary page in json output" );
+$obj = from_json( $res->content );
+is_deeply( $obj, $expected, "Object as expected" ) || diag explain $obj;
+
 # Check user access
 $expected = {
     'ResultSet' => {
@@ -918,3 +990,5 @@
 $obj = from_json( $admin->content );
 Opsview::Test->strip_field_from_hash( "state_duration", $obj );
 is_deeply( $obj, $expected, "Object as expected" ) || diag explain $obj;
+
+done_testing;

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

Reply via email to