icinga2 0.5.2 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +63 -38
- data/doc/downtimes.md +4 -3
- data/doc/hostgroups.md +10 -7
- data/doc/hosts.md +19 -7
- data/doc/notifications.md +24 -7
- data/doc/servicegroups.md +11 -8
- data/doc/services.md +29 -6
- data/doc/usergroups.md +8 -4
- data/doc/users.md +12 -11
- data/examples/test.rb +117 -113
- data/lib/icinga2.rb +71 -61
- data/lib/icinga2/converts.rb +68 -53
- data/lib/icinga2/downtimes.rb +73 -90
- data/lib/icinga2/hostgroups.rb +50 -73
- data/lib/icinga2/hosts.rb +133 -170
- data/lib/icinga2/network.rb +182 -117
- data/lib/icinga2/notifications.rb +82 -79
- data/lib/icinga2/servicegroups.rb +50 -71
- data/lib/icinga2/services.rb +144 -162
- data/lib/icinga2/status.rb +32 -32
- data/lib/icinga2/tools.rb +10 -4
- data/lib/icinga2/usergroups.rb +49 -67
- data/lib/icinga2/users.rb +65 -70
- data/lib/icinga2/version.rb +7 -3
- data/lib/logging.rb +10 -6
- metadata +2 -2
| @@ -1,158 +1,161 @@ | |
| 1 1 |  | 
| 2 | 
            +
            # frozen_string_literal: true
         | 
| 3 | 
            +
             | 
| 2 4 | 
             
            module Icinga2
         | 
| 3 5 |  | 
| 6 | 
            +
              #
         | 
| 7 | 
            +
              #
         | 
| 8 | 
            +
              #
         | 
| 4 9 | 
             
              module Notifications
         | 
| 5 10 |  | 
| 11 | 
            +
                #
         | 
| 12 | 
            +
                #
         | 
| 13 | 
            +
                #
         | 
| 14 | 
            +
                def enable_host_notification( host )
         | 
| 6 15 |  | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 9 | 
            -
                  return hostNotification( { :name => host, :enable_notifications => true } )
         | 
| 16 | 
            +
                  host_notification( name: host, enable_notifications: true )
         | 
| 10 17 | 
             
                end
         | 
| 11 18 |  | 
| 19 | 
            +
                #
         | 
| 20 | 
            +
                #
         | 
| 21 | 
            +
                #
         | 
| 22 | 
            +
                def disable_host_notification( host )
         | 
| 12 23 |  | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
            -
                  return self.hostNotification( { :name => host, :enable_notifications => false } )
         | 
| 24 | 
            +
                  host_notification( name: host, enable_notifications: false )
         | 
| 16 25 | 
             
                end
         | 
| 17 26 |  | 
| 18 | 
            -
             | 
| 19 | 
            -
                 | 
| 27 | 
            +
                #
         | 
| 28 | 
            +
                #
         | 
| 29 | 
            +
                #
         | 
| 30 | 
            +
                def enable_service_notification( params = {} )
         | 
| 20 31 |  | 
| 21 32 | 
             
                  host    = params.get(:host)
         | 
| 22 33 | 
             
                  service = params.get(:service)
         | 
| 23 34 |  | 
| 24 | 
            -
                  if( host | 
| 35 | 
            +
                  if( host.nil? )
         | 
| 25 36 |  | 
| 26 37 | 
             
                    return {
         | 
| 27 | 
            -
                      : | 
| 28 | 
            -
                      : | 
| 38 | 
            +
                      status: 404,
         | 
| 39 | 
            +
                      message: 'missing host name'
         | 
| 29 40 | 
             
                    }
         | 
| 30 41 | 
             
                  end
         | 
| 31 42 |  | 
| 32 | 
            -
                   | 
| 43 | 
            +
                  service_notification( name: host, service: service, enable_notifications: true )
         | 
| 33 44 | 
             
                end
         | 
| 34 45 |  | 
| 46 | 
            +
                #
         | 
| 47 | 
            +
                #
         | 
| 48 | 
            +
                #
         | 
| 49 | 
            +
                def disable_service_notification( host )
         | 
| 35 50 |  | 
| 36 | 
            -
             | 
| 37 | 
            -
             | 
| 38 | 
            -
                  return self.serviceNotification( { :name => host, :enable_notifications => false } )
         | 
| 51 | 
            +
                  service_notification( name: host, enable_notifications: false )
         | 
| 39 52 | 
             
                end
         | 
| 40 53 |  | 
| 54 | 
            +
                #
         | 
| 55 | 
            +
                #
         | 
| 56 | 
            +
                #
         | 
| 57 | 
            +
                def enable_hostgroup_notification( group )
         | 
| 41 58 |  | 
| 42 | 
            -
             | 
| 43 | 
            -
             | 
| 44 | 
            -
                  return self.hostgroupNotification( { :host_group => group, :enable_notifications => true } )
         | 
| 59 | 
            +
                  hostgroup_notification( host_group: group, enable_notifications: true )
         | 
| 45 60 | 
             
                end
         | 
| 46 61 |  | 
| 62 | 
            +
                #
         | 
| 63 | 
            +
                #
         | 
| 64 | 
            +
                #
         | 
| 65 | 
            +
                def disable_hostgroup_notification( group )
         | 
| 47 66 |  | 
| 48 | 
            -
             | 
| 49 | 
            -
             | 
| 50 | 
            -
                  return self.hostgroupNotification( { :host_group => group, :enable_notifications => false } )
         | 
| 67 | 
            +
                  hostgroup_notification( host_group: group, enable_notifications: false )
         | 
| 51 68 | 
             
                end
         | 
| 52 69 |  | 
| 53 | 
            -
             | 
| 54 | 
            -
                 | 
| 70 | 
            +
                #
         | 
| 71 | 
            +
                #
         | 
| 72 | 
            +
                #
         | 
| 73 | 
            +
                def notifications( params = {} )
         | 
| 55 74 |  | 
| 56 75 | 
             
                  name = params.dig(:name)
         | 
| 57 76 |  | 
| 58 | 
            -
                  result = Network.get(  | 
| 59 | 
            -
                    : | 
| 60 | 
            -
                    : | 
| 61 | 
            -
                    : | 
| 62 | 
            -
                    :options  => @options
         | 
| 63 | 
            -
                  } )
         | 
| 77 | 
            +
                  result = Network.get(         host: name,
         | 
| 78 | 
            +
                    url: format( '%s/v1/objects/notifications/%s', @icinga_api_url_base, name ),
         | 
| 79 | 
            +
                    headers: @headers,
         | 
| 80 | 
            +
                    options: @options )
         | 
| 64 81 |  | 
| 65 | 
            -
                   | 
| 82 | 
            +
                  JSON.pretty_generate( result )
         | 
| 66 83 |  | 
| 67 84 | 
             
                end
         | 
| 68 85 |  | 
| 69 86 |  | 
| 70 87 | 
             
                # PRIVATE SECTION
         | 
| 71 88 | 
             
                #
         | 
| 72 | 
            -
                def  | 
| 89 | 
            +
                def host_notification( params = {} )
         | 
| 73 90 |  | 
| 74 91 | 
             
                  name          = params.dig(:name)
         | 
| 75 92 | 
             
                  notifications = params.dig(:enable_notifications) || false
         | 
| 76 93 |  | 
| 77 94 | 
             
                  payload = {
         | 
| 78 | 
            -
                     | 
| 79 | 
            -
                       | 
| 95 | 
            +
                    'attrs'     => {
         | 
| 96 | 
            +
                      'enable_notifications' => notifications
         | 
| 80 97 | 
             
                    }
         | 
| 81 98 | 
             
                  }
         | 
| 82 99 |  | 
| 83 | 
            -
                  result = Network.post(  | 
| 84 | 
            -
                    : | 
| 85 | 
            -
                    : | 
| 86 | 
            -
                    : | 
| 87 | 
            -
                    : | 
| 88 | 
            -
                    :payload => payload
         | 
| 89 | 
            -
                  } )
         | 
| 90 | 
            -
             | 
| 91 | 
            -
                  logger.debug( result.class.to_s )
         | 
| 100 | 
            +
                  result = Network.post(         host: name,
         | 
| 101 | 
            +
                    url: format( '%s/v1/objects/hosts/%s', @icinga_api_url_base, name ),
         | 
| 102 | 
            +
                    headers: @headers,
         | 
| 103 | 
            +
                    options: @options,
         | 
| 104 | 
            +
                    payload: payload )
         | 
| 92 105 |  | 
| 93 | 
            -
                   | 
| 106 | 
            +
                  JSON.pretty_generate( result )
         | 
| 94 107 |  | 
| 95 108 | 
             
                end
         | 
| 96 109 |  | 
| 97 | 
            -
             | 
| 98 | 
            -
                 | 
| 110 | 
            +
                #
         | 
| 111 | 
            +
                #
         | 
| 112 | 
            +
                #
         | 
| 113 | 
            +
                def hostgroup_notification( params = {} )
         | 
| 99 114 |  | 
| 100 115 | 
             
                  group         = params.dig(:host_group)
         | 
| 101 116 | 
             
                  notifications = params.dig(:enable_notifications) || false
         | 
| 102 117 |  | 
| 103 118 | 
             
                  payload = {
         | 
| 104 | 
            -
                     | 
| 105 | 
            -
                     | 
| 106 | 
            -
                       | 
| 119 | 
            +
                    'filter'    => format( '"%s" in host.groups', group ),
         | 
| 120 | 
            +
                    'attrs'     => {
         | 
| 121 | 
            +
                      'enable_notifications' => notifications
         | 
| 107 122 | 
             
                    }
         | 
| 108 123 | 
             
                  }
         | 
| 109 124 |  | 
| 110 | 
            -
                  result = Network.post(  | 
| 111 | 
            -
                    : | 
| 112 | 
            -
                    : | 
| 113 | 
            -
                    : | 
| 114 | 
            -
                    : | 
| 115 | 
            -
                    :payload => payload
         | 
| 116 | 
            -
                  } )
         | 
| 117 | 
            -
             | 
| 118 | 
            -
                  logger.debug( result.class.to_s )
         | 
| 125 | 
            +
                  result = Network.post(         host: name,
         | 
| 126 | 
            +
                    url: format( '%s/v1/objects/services', @icinga_api_url_base ),
         | 
| 127 | 
            +
                    headers: @headers,
         | 
| 128 | 
            +
                    options: @options,
         | 
| 129 | 
            +
                    payload: payload )
         | 
| 119 130 |  | 
| 120 | 
            -
                   | 
| 131 | 
            +
                  JSON.pretty_generate( result )
         | 
| 121 132 |  | 
| 122 133 | 
             
                end
         | 
| 123 134 |  | 
| 124 | 
            -
             | 
| 125 | 
            -
                 | 
| 135 | 
            +
                #
         | 
| 136 | 
            +
                #
         | 
| 137 | 
            +
                #
         | 
| 138 | 
            +
                def service_notification( params = {} )
         | 
| 126 139 |  | 
| 127 140 | 
             
                  name          = params.dig(:name)
         | 
| 128 | 
            -
                  service       = params.dig(:service)
         | 
| 129 141 | 
             
                  notifications = params.dig(:enable_notifications) || false
         | 
| 130 142 |  | 
| 131 143 | 
             
                  payload = {
         | 
| 132 | 
            -
                     | 
| 133 | 
            -
                     | 
| 134 | 
            -
                       | 
| 144 | 
            +
                    'filter'    => format( 'host.name=="%s"', name ),
         | 
| 145 | 
            +
                    'attrs'     => {
         | 
| 146 | 
            +
                      'enable_notifications' => notifications
         | 
| 135 147 | 
             
                    }
         | 
| 136 148 | 
             
                  }
         | 
| 137 149 |  | 
| 138 | 
            -
                   | 
| 139 | 
            -
             | 
| 140 | 
            -
             | 
| 141 | 
            -
             | 
| 142 | 
            -
                    : | 
| 143 | 
            -
                    :url     => sprintf( '%s/v1/objects/services', @icingaApiUrlBase ),
         | 
| 144 | 
            -
                    :headers => @headers,
         | 
| 145 | 
            -
                    :options => @options,
         | 
| 146 | 
            -
                    :payload => payload
         | 
| 147 | 
            -
                  } )
         | 
| 148 | 
            -
             | 
| 149 | 
            -
                  logger.debug( result.class.to_s )
         | 
| 150 | 
            +
                  result = Network.post(         host: name,
         | 
| 151 | 
            +
                    url: format( '%s/v1/objects/services', @icinga_api_url_base ),
         | 
| 152 | 
            +
                    headers: @headers,
         | 
| 153 | 
            +
                    options: @options,
         | 
| 154 | 
            +
                    payload: payload )
         | 
| 150 155 |  | 
| 151 | 
            -
                   | 
| 156 | 
            +
                  JSON.pretty_generate( result )
         | 
| 152 157 |  | 
| 153 158 | 
             
                end
         | 
| 154 159 |  | 
| 155 | 
            -
             | 
| 156 160 | 
             
              end
         | 
| 157 | 
            -
             | 
| 158 161 | 
             
            end
         | 
| @@ -1,101 +1,80 @@ | |
| 1 1 |  | 
| 2 | 
            +
            # frozen_string_literal: true
         | 
| 3 | 
            +
             | 
| 2 4 | 
             
            module Icinga2
         | 
| 3 5 |  | 
| 6 | 
            +
              #
         | 
| 7 | 
            +
              #
         | 
| 8 | 
            +
              #
         | 
| 4 9 | 
             
              module Servicegroups
         | 
| 5 10 |  | 
| 6 | 
            -
             | 
| 7 | 
            -
                 | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
                   | 
| 11 | 
            -
             | 
| 12 | 
            -
                  if( name | 
| 13 | 
            -
             | 
| 11 | 
            +
                #
         | 
| 12 | 
            +
                #
         | 
| 13 | 
            +
                #
         | 
| 14 | 
            +
                def add_servicegroup( params = {} )
         | 
| 15 | 
            +
                  name = params.dig(:name)
         | 
| 16 | 
            +
                  display_name = params.dig(:display_name)
         | 
| 17 | 
            +
                  if( name.nil? )
         | 
| 14 18 | 
             
                    return {
         | 
| 15 | 
            -
                      : | 
| 16 | 
            -
                      : | 
| 19 | 
            +
                      status: 404,
         | 
| 20 | 
            +
                      message: 'missing servicegroup name'
         | 
| 17 21 | 
             
                    }
         | 
| 18 22 | 
             
                  end
         | 
| 19 23 |  | 
| 20 | 
            -
                  payload = {
         | 
| 21 | 
            -
                    "attrs" => {
         | 
| 22 | 
            -
                      "display_name"         => displayName
         | 
| 23 | 
            -
                    }
         | 
| 24 | 
            -
                  }
         | 
| 25 | 
            -
             | 
| 26 | 
            -
                  result = Network.put( {
         | 
| 27 | 
            -
                    :host    => name,
         | 
| 28 | 
            -
                    :url     => sprintf( '%s/v1/objects/servicegroups/%s', @icingaApiUrlBase, name ),
         | 
| 29 | 
            -
                    :headers => @headers,
         | 
| 30 | 
            -
                    :options => @options,
         | 
| 31 | 
            -
                    :payload => payload
         | 
| 32 | 
            -
                  } )
         | 
| 24 | 
            +
                  payload = { 'attrs' => { 'display_name' => display_name } }
         | 
| 33 25 |  | 
| 34 | 
            -
                   | 
| 26 | 
            +
                  result = Network.put(         host: name,
         | 
| 27 | 
            +
                    url: format( '%s/v1/objects/servicegroups/%s', @icinga_api_url_base, name ),
         | 
| 28 | 
            +
                    headers: @headers,
         | 
| 29 | 
            +
                    options: @options,
         | 
| 30 | 
            +
                    payload: payload )
         | 
| 35 31 |  | 
| 32 | 
            +
                  JSON.pretty_generate( result )
         | 
| 36 33 | 
             
                end
         | 
| 37 34 |  | 
| 38 | 
            -
             | 
| 39 | 
            -
                 | 
| 40 | 
            -
             | 
| 35 | 
            +
                #
         | 
| 36 | 
            +
                #
         | 
| 37 | 
            +
                #
         | 
| 38 | 
            +
                def delete_servicegroup( params = {} )
         | 
| 41 39 | 
             
                  name = params.dig(:name)
         | 
| 42 | 
            -
             | 
| 43 | 
            -
                  if( name == nil )
         | 
| 44 | 
            -
             | 
| 40 | 
            +
                  if( name.nil? )
         | 
| 45 41 | 
             
                    return {
         | 
| 46 | 
            -
                      : | 
| 47 | 
            -
                      : | 
| 42 | 
            +
                      status: 404,
         | 
| 43 | 
            +
                      message: 'missing servicegroup name'
         | 
| 48 44 | 
             
                    }
         | 
| 49 45 | 
             
                  end
         | 
| 50 46 |  | 
| 51 | 
            -
                  result = Network.delete(  | 
| 52 | 
            -
                    : | 
| 53 | 
            -
                    : | 
| 54 | 
            -
                    : | 
| 55 | 
            -
                    :options => @options
         | 
| 56 | 
            -
                  } )
         | 
| 57 | 
            -
             | 
| 58 | 
            -
                  return JSON.pretty_generate( result )
         | 
| 47 | 
            +
                  result = Network.delete(         host: name,
         | 
| 48 | 
            +
                    url: format( '%s/v1/objects/servicegroups/%s?cascade=1', @icinga_api_url_base, name ),
         | 
| 49 | 
            +
                    headers: @headers,
         | 
| 50 | 
            +
                    options: @options )
         | 
| 59 51 |  | 
| 52 | 
            +
                  JSON.pretty_generate( result )
         | 
| 60 53 | 
             
                end
         | 
| 61 54 |  | 
| 62 | 
            -
             | 
| 63 | 
            -
                 | 
| 64 | 
            -
             | 
| 55 | 
            +
                #
         | 
| 56 | 
            +
                #
         | 
| 57 | 
            +
                #
         | 
| 58 | 
            +
                def servicegroups( params = {} )
         | 
| 65 59 | 
             
                  name = params.dig(:name)
         | 
| 60 | 
            +
                  result = Network.get(         host: name,
         | 
| 61 | 
            +
                    url: format( '%s/v1/objects/servicegroups/%s', @icinga_api_url_base, name ),
         | 
| 62 | 
            +
                    headers: @headers,
         | 
| 63 | 
            +
                    options: @options )
         | 
| 66 64 |  | 
| 67 | 
            -
                   | 
| 68 | 
            -
                    :host     => name,
         | 
| 69 | 
            -
                    :url      => sprintf( '%s/v1/objects/servicegroups/%s', @icingaApiUrlBase, name ),
         | 
| 70 | 
            -
                    :headers  => @headers,
         | 
| 71 | 
            -
                    :options  => @options
         | 
| 72 | 
            -
                  } )
         | 
| 73 | 
            -
             | 
| 74 | 
            -
                  return JSON.pretty_generate( result )
         | 
| 75 | 
            -
             | 
| 65 | 
            +
                  JSON.pretty_generate( result )
         | 
| 76 66 | 
             
                end
         | 
| 77 67 |  | 
| 78 | 
            -
             | 
| 79 | 
            -
                 | 
| 80 | 
            -
             | 
| 81 | 
            -
             | 
| 82 | 
            -
                  result =  | 
| 83 | 
            -
             | 
| 84 | 
            -
                  if( result.is_a?( String ) )
         | 
| 85 | 
            -
                    result = JSON.parse( result )
         | 
| 86 | 
            -
                  end
         | 
| 87 | 
            -
             | 
| 68 | 
            +
                #
         | 
| 69 | 
            +
                #
         | 
| 70 | 
            +
                #
         | 
| 71 | 
            +
                def exists_servicegroup?( name )
         | 
| 72 | 
            +
                  result = servicegroups( name: name )
         | 
| 73 | 
            +
                  result = JSON.parse( result ) if  result.is_a?( String )
         | 
| 88 74 | 
             
                  status = result.dig('status')
         | 
| 89 | 
            -
             | 
| 90 | 
            -
                   | 
| 91 | 
            -
                    return true
         | 
| 92 | 
            -
                  end
         | 
| 93 | 
            -
             | 
| 94 | 
            -
                  return false
         | 
| 95 | 
            -
             | 
| 75 | 
            +
                  return true if  !status.nil? && status == 200
         | 
| 76 | 
            +
                  false
         | 
| 96 77 | 
             
                end
         | 
| 97 78 |  | 
| 98 | 
            -
             | 
| 99 79 | 
             
              end
         | 
| 100 | 
            -
             | 
| 101 80 | 
             
            end
         | 
    
        data/lib/icinga2/services.rb
    CHANGED
    
    | @@ -1,34 +1,23 @@ | |
| 1 1 |  | 
| 2 | 
            +
            # frozen_string_literal: true
         | 
| 3 | 
            +
             | 
| 2 4 | 
             
            module Icinga2
         | 
| 3 5 |  | 
| 6 | 
            +
              #
         | 
| 7 | 
            +
              #
         | 
| 8 | 
            +
              #
         | 
| 4 9 | 
             
              module Services
         | 
| 5 10 |  | 
| 6 | 
            -
                 | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
                    hash.each do |k, v|
         | 
| 11 | 
            -
             | 
| 12 | 
            -
                      if( k == "host" && v.is_a?( String ) )
         | 
| 13 | 
            -
                        v.replace( host )
         | 
| 14 | 
            -
             | 
| 15 | 
            -
                      elsif( v.is_a?( Hash ) )
         | 
| 16 | 
            -
                        self.updateHost( v, host )
         | 
| 17 | 
            -
             | 
| 18 | 
            -
                      elsif( v.is_a?(Array) )
         | 
| 19 | 
            -
             | 
| 20 | 
            -
                        v.flatten.each { |x| self.updateHost( x, host ) if x.is_a?( Hash ) }
         | 
| 21 | 
            -
                      end
         | 
| 22 | 
            -
                    end
         | 
| 23 | 
            -
             | 
| 24 | 
            -
                    hash
         | 
| 25 | 
            -
                  end
         | 
| 11 | 
            +
                #
         | 
| 12 | 
            +
                #
         | 
| 13 | 
            +
                #
         | 
| 14 | 
            +
                def add_services( host, services = {} )
         | 
| 26 15 |  | 
| 27 16 | 
             
                  services.each do |s,v|
         | 
| 28 17 |  | 
| 29 18 | 
             
                    payload = {
         | 
| 30 | 
            -
                       | 
| 31 | 
            -
                       | 
| 19 | 
            +
                      'templates' => [ 'generic-service' ],
         | 
| 20 | 
            +
                      'attrs'     => update_host( v, host )
         | 
| 32 21 | 
             
                    }
         | 
| 33 22 |  | 
| 34 23 | 
             
                    logger.debug( s )
         | 
| @@ -36,13 +25,11 @@ module Icinga2 | |
| 36 25 |  | 
| 37 26 | 
             
                    logger.debug( JSON.pretty_generate( payload ) )
         | 
| 38 27 |  | 
| 39 | 
            -
                    result = Network.put(  | 
| 40 | 
            -
                      : | 
| 41 | 
            -
                      : | 
| 42 | 
            -
                      : | 
| 43 | 
            -
                      : | 
| 44 | 
            -
                      :payload => payload
         | 
| 45 | 
            -
                    })
         | 
| 28 | 
            +
                    result = Network.put(           host: host,
         | 
| 29 | 
            +
                      url: format( '%s/v1/objects/services/%s!%s', @icinga_api_url_base, host, s ),
         | 
| 30 | 
            +
                      headers: @headers,
         | 
| 31 | 
            +
                      options: @options,
         | 
| 32 | 
            +
                      payload: payload)
         | 
| 46 33 |  | 
| 47 34 | 
             
                    logger.debug( result )
         | 
| 48 35 |  | 
| @@ -50,8 +37,10 @@ module Icinga2 | |
| 50 37 |  | 
| 51 38 | 
             
                end
         | 
| 52 39 |  | 
| 53 | 
            -
             | 
| 54 | 
            -
                 | 
| 40 | 
            +
                #
         | 
| 41 | 
            +
                #
         | 
| 42 | 
            +
                #
         | 
| 43 | 
            +
                def unhandled_services( params = {} )
         | 
| 55 44 |  | 
| 56 45 | 
             
                  # taken from https://blog.netways.de/2016/11/18/icinga-2-api-cheat-sheet/
         | 
| 57 46 | 
             
                  # 5) Anzeige aller Services die unhandled sind und weder in Downtime, noch acknowledged sind
         | 
| @@ -59,108 +48,100 @@ module Icinga2 | |
| 59 48 |  | 
| 60 49 | 
             
                end
         | 
| 61 50 |  | 
| 62 | 
            -
             | 
| 63 | 
            -
                 | 
| 51 | 
            +
                #
         | 
| 52 | 
            +
                #
         | 
| 53 | 
            +
                #
         | 
| 54 | 
            +
                def services( params = {} )
         | 
| 64 55 |  | 
| 65 56 | 
             
                  name    = params.dig(:host)
         | 
| 66 57 | 
             
                  service = params.dig(:service)
         | 
| 67 58 |  | 
| 68 | 
            -
                  if( service | 
| 69 | 
            -
                     | 
| 59 | 
            +
                  url = if( service.nil? )
         | 
| 60 | 
            +
                    format( '%s/v1/objects/services/%s', @icinga_api_url_base, name )
         | 
| 70 61 | 
             
                  else
         | 
| 71 | 
            -
                     | 
| 72 | 
            -
             | 
| 62 | 
            +
                    format( '%s/v1/objects/services/%s!%s', @icinga_api_url_base, name, service )
         | 
| 63 | 
            +
                        end
         | 
| 73 64 |  | 
| 74 | 
            -
                  result = Network.get(  | 
| 75 | 
            -
                    : | 
| 76 | 
            -
                    : | 
| 77 | 
            -
                    : | 
| 78 | 
            -
                    :options  => @options
         | 
| 79 | 
            -
                  } )
         | 
| 65 | 
            +
                  result = Network.get(         host: name,
         | 
| 66 | 
            +
                    url: url,
         | 
| 67 | 
            +
                    headers: @headers,
         | 
| 68 | 
            +
                    options: @options )
         | 
| 80 69 |  | 
| 81 | 
            -
                   | 
| 70 | 
            +
                  JSON.pretty_generate( result )
         | 
| 82 71 |  | 
| 83 72 | 
             
                end
         | 
| 84 73 |  | 
| 85 | 
            -
             | 
| 86 | 
            -
                 | 
| 74 | 
            +
                #
         | 
| 75 | 
            +
                #
         | 
| 76 | 
            +
                #
         | 
| 77 | 
            +
                def exists_service?( params = {} )
         | 
| 87 78 |  | 
| 88 79 | 
             
                  host    = params.dig(:host)
         | 
| 89 80 | 
             
                  service = params.dig(:service)
         | 
| 90 81 |  | 
| 91 | 
            -
                  if( host | 
| 82 | 
            +
                  if( host.nil? )
         | 
| 92 83 |  | 
| 93 84 | 
             
                    return {
         | 
| 94 | 
            -
                      : | 
| 95 | 
            -
                      : | 
| 85 | 
            +
                      status: 404,
         | 
| 86 | 
            +
                      message: 'missing host name'
         | 
| 96 87 | 
             
                    }
         | 
| 97 88 | 
             
                  end
         | 
| 98 89 |  | 
| 99 | 
            -
                  result =  | 
| 90 | 
            +
                  result = services( host: host, service: service )
         | 
| 100 91 |  | 
| 101 | 
            -
                   | 
| 102 | 
            -
                    result = JSON.parse( result )
         | 
| 103 | 
            -
                  end
         | 
| 92 | 
            +
                  result = JSON.parse( result ) if  result.is_a?( String )
         | 
| 104 93 |  | 
| 105 94 | 
             
                  status = result.dig('status')
         | 
| 106 95 |  | 
| 107 | 
            -
                  if | 
| 108 | 
            -
                    return true
         | 
| 109 | 
            -
                  end
         | 
| 96 | 
            +
                  return true if  !status.nil? && status == 200
         | 
| 110 97 |  | 
| 111 | 
            -
                   | 
| 98 | 
            +
                  false
         | 
| 112 99 |  | 
| 113 100 | 
             
                end
         | 
| 114 101 |  | 
| 115 | 
            -
             | 
| 116 | 
            -
                 | 
| 102 | 
            +
                #
         | 
| 103 | 
            +
                #
         | 
| 104 | 
            +
                #
         | 
| 105 | 
            +
                def service_objects( params = {} )
         | 
| 117 106 |  | 
| 118 107 | 
             
                  attrs   = params.dig(:attrs)
         | 
| 119 108 | 
             
                  filter  = params.dig(:filter)
         | 
| 120 109 | 
             
                  joins   = params.dig(:joins)
         | 
| 121 110 | 
             
                  payload = {}
         | 
| 122 111 |  | 
| 123 | 
            -
                  if( attrs | 
| 124 | 
            -
                    attrs = [ | 
| 112 | 
            +
                  if( attrs.nil? )
         | 
| 113 | 
            +
                    attrs = %w[name state acknowledgement downtime_depth last_check]
         | 
| 125 114 | 
             
                  end
         | 
| 126 115 |  | 
| 127 | 
            -
                  if( joins | 
| 116 | 
            +
                  if( joins.nil? )
         | 
| 128 117 | 
             
                    joins = ['host.name','host.state','host.acknowledgement','host.downtime_depth','host.last_check']
         | 
| 129 118 | 
             
                  end
         | 
| 130 119 |  | 
| 131 | 
            -
                   | 
| 132 | 
            -
                    payload['attrs'] = attrs
         | 
| 133 | 
            -
                  end
         | 
| 120 | 
            +
                  payload['attrs'] = attrs unless  attrs.nil?
         | 
| 134 121 |  | 
| 135 | 
            -
                   | 
| 136 | 
            -
                    payload['filter'] = filter
         | 
| 137 | 
            -
                  end
         | 
| 122 | 
            +
                  payload['filter'] = filter unless  filter.nil?
         | 
| 138 123 |  | 
| 139 | 
            -
                   | 
| 140 | 
            -
                    payload['joins'] = joins
         | 
| 141 | 
            -
                  end
         | 
| 124 | 
            +
                  payload['joins'] = joins unless  joins.nil?
         | 
| 142 125 |  | 
| 143 | 
            -
                  result = Network.get(  | 
| 144 | 
            -
                    : | 
| 145 | 
            -
                    : | 
| 146 | 
            -
                    : | 
| 147 | 
            -
                    : | 
| 148 | 
            -
                    :payload  => payload
         | 
| 149 | 
            -
                  } )
         | 
| 126 | 
            +
                  result = Network.get(         host: nil,
         | 
| 127 | 
            +
                    url: format( '%s/v1/objects/services', @icinga_api_url_base ),
         | 
| 128 | 
            +
                    headers: @headers,
         | 
| 129 | 
            +
                    options: @options,
         | 
| 130 | 
            +
                    payload: payload )
         | 
| 150 131 |  | 
| 151 | 
            -
                   | 
| 132 | 
            +
                  JSON.pretty_generate( result )
         | 
| 152 133 |  | 
| 153 134 | 
             
                end
         | 
| 154 135 |  | 
| 136 | 
            +
                #
         | 
| 137 | 
            +
                #
         | 
| 138 | 
            +
                #
         | 
| 139 | 
            +
                def service_problems
         | 
| 155 140 |  | 
| 156 | 
            -
             | 
| 157 | 
            -
             | 
| 158 | 
            -
                  data     = self.serviceObjects()
         | 
| 141 | 
            +
                  data     = service_objects
         | 
| 159 142 | 
             
                  problems = 0
         | 
| 160 143 |  | 
| 161 | 
            -
                   | 
| 162 | 
            -
                    data = JSON.parse(data)
         | 
| 163 | 
            -
                  end
         | 
| 144 | 
            +
                  data = JSON.parse(data) if  data.is_a?(String)
         | 
| 164 145 |  | 
| 165 146 | 
             
                  nodes = data.dig('nodes')
         | 
| 166 147 |  | 
| @@ -169,133 +150,134 @@ module Icinga2 | |
| 169 150 | 
             
                    attrs           = n.last.dig('attrs')
         | 
| 170 151 |  | 
| 171 152 | 
             
                    state           = attrs.dig('state')
         | 
| 172 | 
            -
                     | 
| 153 | 
            +
                    downtime_depth   = attrs.dig('downtime_depth')
         | 
| 173 154 | 
             
                    acknowledgement = attrs.dig('acknowledgement')
         | 
| 174 155 |  | 
| 175 156 | 
             
            #         puts state
         | 
| 176 157 |  | 
| 177 | 
            -
                    if( state != 0 &&  | 
| 158 | 
            +
                    if( state != 0 && downtime_depth.zero? && acknowledgement.zero? )
         | 
| 178 159 | 
             
                      problems += 1 #= problems +1
         | 
| 179 160 | 
             
                    end
         | 
| 180 161 |  | 
| 181 162 | 
             
                  end
         | 
| 182 163 |  | 
| 183 | 
            -
                   | 
| 164 | 
            +
                  problems
         | 
| 184 165 |  | 
| 185 166 | 
             
                end
         | 
| 186 167 |  | 
| 168 | 
            +
                #
         | 
| 169 | 
            +
                #
         | 
| 170 | 
            +
                #
         | 
| 171 | 
            +
                def problem_services( max_items = 5 )
         | 
| 187 172 |  | 
| 188 | 
            -
             | 
| 189 | 
            -
             | 
| 190 | 
            -
                  count = 0
         | 
| 191 | 
            -
                  @serviceProblems = {}
         | 
| 192 | 
            -
                  @serviceProblemsSeverity = {}
         | 
| 173 | 
            +
                  @service_problems = {}
         | 
| 174 | 
            +
                  @service_problems_severity = {}
         | 
| 193 175 |  | 
| 194 176 | 
             
                  # only fetch the minimal attribute set required for severity calculation
         | 
| 195 | 
            -
                   | 
| 177 | 
            +
                  services_data = service_objects
         | 
| 196 178 |  | 
| 197 | 
            -
                  if(  | 
| 179 | 
            +
                  if( services_data.is_a?(String) )
         | 
| 198 180 |  | 
| 199 | 
            -
                     | 
| 181 | 
            +
                    services_data = JSON.parse( services_data )
         | 
| 200 182 | 
             
                  end
         | 
| 201 183 |  | 
| 202 | 
            -
                   | 
| 184 | 
            +
                  services_data = services_data.dig('nodes')
         | 
| 203 185 |  | 
| 204 | 
            -
                   | 
| 186 | 
            +
                  services_data.each do |_service,v|
         | 
| 205 187 |  | 
| 206 188 | 
             
                    name  = v.dig('name')
         | 
| 207 189 | 
             
                    state = v.dig('attrs','state')
         | 
| 208 190 | 
             
            #         logger.debug( "Severity for #{name}" )
         | 
| 209 | 
            -
                    if | 
| 210 | 
            -
                      next
         | 
| 211 | 
            -
                    end
         | 
| 191 | 
            +
                    next if  state.zero?
         | 
| 212 192 |  | 
| 213 | 
            -
                    @ | 
| 193 | 
            +
                    @service_problems[name] = service_severity(v)
         | 
| 214 194 | 
             
                  end
         | 
| 215 195 |  | 
| 216 | 
            -
                  @ | 
| 217 | 
            -
             | 
| 218 | 
            -
                  @serviceProblems.keys[1..max_items].each { |k,v| @serviceProblemsSeverity[k] = @serviceProblems[k] }
         | 
| 196 | 
            +
                  @service_problems.sort.reverse!
         | 
| 219 197 |  | 
| 220 | 
            -
             | 
| 221 | 
            -
            #
         | 
| 222 | 
            -
            #         if( count >= max_items )
         | 
| 223 | 
            -
            #           break
         | 
| 224 | 
            -
            #         end
         | 
| 225 | 
            -
            #
         | 
| 226 | 
            -
            #         @serviceProblemsSeverity[k] = v
         | 
| 227 | 
            -
            #
         | 
| 228 | 
            -
            #         count += 1
         | 
| 229 | 
            -
            #       end
         | 
| 198 | 
            +
                  @service_problems.keys[1..max_items].each { |k,_v| @service_problems_severity[k] = @service_problems[k] }
         | 
| 230 199 |  | 
| 231 | 
            -
                   | 
| 200 | 
            +
                  @service_problems_severity
         | 
| 232 201 | 
             
                end
         | 
| 233 202 |  | 
| 203 | 
            +
                #
         | 
| 204 | 
            +
                #
         | 
| 205 | 
            +
                #
         | 
| 206 | 
            +
                def update_host( hash, host )
         | 
| 207 | 
            +
             | 
| 208 | 
            +
                  hash.each do |k, v|
         | 
| 209 | 
            +
             | 
| 210 | 
            +
                    if( k == 'host' && v.is_a?( String ) )
         | 
| 211 | 
            +
                      v.replace( host )
         | 
| 212 | 
            +
             | 
| 213 | 
            +
                    elsif( v.is_a?( Hash ) )
         | 
| 214 | 
            +
                      update_host( v, host )
         | 
| 215 | 
            +
             | 
| 216 | 
            +
                    elsif( v.is_a?(Array) )
         | 
| 217 | 
            +
             | 
| 218 | 
            +
                      v.flatten.each { |x| update_host( x, host ) if x.is_a?( Hash ) }
         | 
| 219 | 
            +
                    end
         | 
| 220 | 
            +
                  end
         | 
| 221 | 
            +
             | 
| 222 | 
            +
                  hash
         | 
| 223 | 
            +
                end
         | 
| 234 224 |  | 
| 235 225 | 
             
                # private
         | 
| 236 226 | 
             
                # stolen from Icinga Web 2
         | 
| 237 227 | 
             
                # ./modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicestatusQuery.php
         | 
| 238 228 | 
             
                #
         | 
| 239 | 
            -
                def  | 
| 229 | 
            +
                def service_severity(service)
         | 
| 240 230 |  | 
| 241 | 
            -
                  attrs | 
| 231 | 
            +
                  attrs           = service.dig('attrs')
         | 
| 242 232 | 
             
                  state           = attrs.dig('state')
         | 
| 243 | 
            -
                  acknowledgement = attrs.dig('acknowledgement')
         | 
| 244 | 
            -
                   | 
| 245 | 
            -
             | 
| 246 | 
            -
            #       logger.debug( attrs )
         | 
| 233 | 
            +
                  acknowledgement = attrs.dig('acknowledgement') || 0
         | 
| 234 | 
            +
                  downtime_depth  = attrs.dig('downtime_depth')  || 0
         | 
| 247 235 |  | 
| 248 236 | 
             
                  severity = 0
         | 
| 249 237 |  | 
| 250 | 
            -
                   | 
| 251 | 
            -
             | 
| 252 | 
            -
             | 
| 253 | 
            -
             | 
| 254 | 
            -
             | 
| 255 | 
            -
             | 
| 256 | 
            -
                    if( acknowledgement != 0 )
         | 
| 257 | 
            -
                      severity += 2
         | 
| 258 | 
            -
                    elsif( downtimeDepth > 0 )
         | 
| 259 | 
            -
                      severity += 1
         | 
| 260 | 
            -
                    else
         | 
| 261 | 
            -
                      severity += 4
         | 
| 262 | 
            -
                    end
         | 
| 263 | 
            -
                  else
         | 
| 264 | 
            -
                    if( self.getObjectHasBeenChecked( service ) )
         | 
| 265 | 
            -
                      severity += 16
         | 
| 266 | 
            -
                    elsif( state == 1 )
         | 
| 267 | 
            -
                      severity += 32
         | 
| 268 | 
            -
                    elsif( state == 2 )
         | 
| 269 | 
            -
                      severity += 128
         | 
| 270 | 
            -
                    elsif( state == 3 )
         | 
| 271 | 
            -
                      severity += 64
         | 
| 238 | 
            +
                  severity +=
         | 
| 239 | 
            +
                    if acknowledgement != 0
         | 
| 240 | 
            +
                      2
         | 
| 241 | 
            +
                    elsif downtime_depth > 0
         | 
| 242 | 
            +
                      1
         | 
| 272 243 | 
             
                    else
         | 
| 273 | 
            -
                       | 
| 244 | 
            +
                      4
         | 
| 274 245 | 
             
                    end
         | 
| 275 246 |  | 
| 247 | 
            +
                  severity += 16 if object_has_been_checked?(service)
         | 
| 248 | 
            +
             | 
| 249 | 
            +
                  unless state.zero?
         | 
| 250 | 
            +
             | 
| 251 | 
            +
                    severity +=
         | 
| 252 | 
            +
                      if state == 1
         | 
| 253 | 
            +
                        32
         | 
| 254 | 
            +
                      elsif state == 2
         | 
| 255 | 
            +
                        64
         | 
| 256 | 
            +
                      else
         | 
| 257 | 
            +
                        256
         | 
| 258 | 
            +
                      end
         | 
| 259 | 
            +
             | 
| 276 260 | 
             
                    # requires joins
         | 
| 277 261 | 
             
                    host_attrs = service.dig('joins','host')
         | 
| 278 | 
            -
             | 
| 279 | 
            -
                    host_state = host_attrs.dig('state')
         | 
| 262 | 
            +
                    host_state           = host_attrs.dig('state')
         | 
| 280 263 | 
             
                    host_acknowledgement = host_attrs.dig('acknowledgement')
         | 
| 281 | 
            -
                     | 
| 282 | 
            -
             | 
| 283 | 
            -
                     | 
| 284 | 
            -
                       | 
| 285 | 
            -
             | 
| 286 | 
            -
                       | 
| 287 | 
            -
             | 
| 288 | 
            -
                       | 
| 289 | 
            -
             | 
| 290 | 
            -
                       | 
| 291 | 
            -
             | 
| 264 | 
            +
                    host_downtime_depth  = host_attrs.dig('downtime_depth')
         | 
| 265 | 
            +
             | 
| 266 | 
            +
                    severity +=
         | 
| 267 | 
            +
                      if host_state > 0
         | 
| 268 | 
            +
                        1024
         | 
| 269 | 
            +
                      elsif host_acknowledgement
         | 
| 270 | 
            +
                        512
         | 
| 271 | 
            +
                      elsif host_downtime_depth > 0
         | 
| 272 | 
            +
                        256
         | 
| 273 | 
            +
                      else
         | 
| 274 | 
            +
                        2048
         | 
| 275 | 
            +
                      end
         | 
| 276 | 
            +
             | 
| 292 277 | 
             
                  end
         | 
| 293 278 |  | 
| 294 | 
            -
                   | 
| 279 | 
            +
                  severity
         | 
| 295 280 | 
             
                end
         | 
| 296 281 |  | 
| 297 | 
            -
             | 
| 298 | 
            -
             | 
| 299 282 | 
             
              end
         | 
| 300 | 
            -
             | 
| 301 283 | 
             
            end
         |