Enabling Maintenance Windows in Event Rules

This section shows you the steps necessary to enable Maintenance Windows in Event Applications rules by:

Dependencies

Best Practices

Modifying code in LoadRules

  1. In the LoadRules file, add or uncomment the following code:

    #=======================================
    # Device Maintenance Window Example
    #=======================================
    my $WindowSQL = "
           SELECT D.CustomName,
                  INET_NTOA(D.IPAddress) AS IPv4,
                  INET6_NTOA(D.IPv6Address) AS IPv6,
                  D.DNSName AS DNS,
                  DSI.SysName,
                  W.StartTime,
                  W.StopTime
             FROM DeviceWindows_Devices AS DWD
        LEFT JOIN Devices AS D
               ON D.DeviceID = DWD.DeviceID
        LEFT JOIN DeviceWindows AS W
               ON DWD.WindowID = W.WindowID
        LEFT JOIN DeviceSystemInfo AS DSI
               ON D.DeviceID = DSI.DeviceID
    ";
    $DBH = DBConnect($Config, 'Assure1', {AutoCommit => 1});
    my $Count = 0;
    my $SelectStatement = $DBH->prepare($WindowSQL);
    $SelectStatement->execute();
    while (my $ref = $SelectStatement->fetchrow_hashref()) {
        $WindowHash->{$ref->{CustomName}}->{StartTime} = $ref->{StartTime};
        $WindowHash->{$ref->{CustomName}}->{StopTime} = $ref->{StopTime};
    
        $WindowHash->{$ref->{IPv4}}->{StartTime} = $ref->{StartTime};
        $WindowHash->{$ref->{IPv4}}->{StopTime} = $ref->{StopTime};
    
        $WindowHash->{$ref->{IPv6}}->{StartTime} = $ref->{StartTime};
        $WindowHash->{$ref->{IPv6}}->{StopTime} = $ref->{StopTime};
    
        $WindowHash->{$ref->{DNS}}->{StartTime} = $ref->{StartTime};
        $WindowHash->{$ref->{DNS}}->{StopTime} = $ref->{StopTime};
    
        $WindowHash->{$ref->{SysName}}->{StartTime} = $ref->{StartTime};
        $WindowHash->{$ref->{SysName}}->{StopTime} = $ref->{StopTime};
    
        $Count++;
    }
    $WindowHash->{''} = 0;
    $SelectStatement->finish();
    $DBH->disconnect;
    
    $Log->Message('INFO',"Device Maintenance Windows - Found [$Count] Devices/Windows");
    $Log->Message('DEBUG',"Device Maintenance Windows - Device Dump\n-------------\n" . Dumper($WindowHash) . "\n-------------");
    #--------------------------------------
    
  2. Check the code syntax.

  3. Save the file.

Modifying Code in BaseRules

  1. In the BaseRules file, add the following near the bottom of the file so this is the last processing step that is done:

    ### ORIGINAL PROCESSING IS ABOVE...
    
    #========================
    # Device Window Suppression -- Used in Base or Include Rules
    #========================
    my $StartWindow = int($WindowHash->{$Event->{'Node'}}->{StartTime} || $WindowHash->{$Event->{'IPAddress'}}->{StartTime});
    my $EndWindow   = int($WindowHash->{$Event->{'Node'}}->{StopTime}  || $WindowHash->{$Event->{'IPAddress'}}->{StopTime});
    my $CurrentTime = time();
    if (($StartWindow <= $CurrentTime) && ($EndWindow >= $CurrentTime)) {
        $Event->{'Severity'} = 0;
        $Event->{'Custom5'}  = "Under Maintenance";
    }
    #------------------------
    
  2. Edit the code if you wish to customize it further.

  3. Check the code syntax.

  4. Save the file.

  5. Restart the service or use the Reload Config option, then verify via the logs that the maintenance window(s) are loaded. Below is an example log based on the above logic, but will only be logged if using an INFO or DEBUG logging level.

  6. If a device has a duplicate value (for example, the same DNS Name and Sys Name), the hash will only have a single reference to that name.

    [DATE TIME] [INFO]   Device Maintenance Windows - Found [2] Devices/Windows
    [DATE TIME] [DEBUG]  Device Maintenance Windows - Device Dump
    -------------
    $VAR1 = {
              '' => 0,
              'Device_1_DNS_Name' => {
                                       'StartTime' => '(Window Start Time)',
                                       'StopTime' => '(Window Stop Time)'
                                     },
              'Device_1_Custom_Name' => {
                                          'StartTime' => '(Window Start Time)',
                                          'StopTime' => '(Window Stop Time)'
                                        },
              'Device_1_Sys_Name' => {
                                       'StartTime' => '(Window Start Time)',
                                       'StopTime' => '(Window Stop Time)'
                                     },
              'Device_1_IPv4_Address' => {
                                           'StartTime' => '(Window Start Time)',
                                           'StopTime' => '(Window Stop Time)',
                                         },
              'Device_1_IPv6_Address' => {
                                           'StartTime' => '(Window Start Time)',
                                           'StopTime' => '(Window Stop Time)'
                                         },
              'Device_2_DNS_Name' => {
                                       'StartTime' => '(Window Start Time)',
                                       'StopTime' => '(Window Stop Time)'
                                     },
              'Device_2_Custom_Name' => {
                                          'StartTime' => '(Window Start Time)',
                                          'StopTime' => '(Window Stop Time)'
                                        },
              'Device_2_Sys_Name' => {
                                       'StartTime' => '(Window Start Time)',
                                       'StopTime' => '(Window Stop Time)'
                                     },
              'Device_2_IPv4_Address' => {
                                           'StartTime' => '(Window Start Time)',
                                           'StopTime' => '(Window Stop Time)',
                                         },
              'Device_2_IPv6_Address' => {
                                           'StartTime' => '(Window Start Time)',
                                           'StopTime' => '(Window Stop Time)'
                                         },
              ...
            };
    -------------