Module: Icinga2::Downtimes

Included in:
Client
Defined in:
lib/icinga2/downtimes.rb

Instance Method Summary collapse

Instance Method Details

#addDowntime(params = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/icinga2/downtimes.rb', line 7

def addDowntime( params = {} )

  name            = params.dig(:name)
  hostName        = params.dig(:host)
#      serviceName     = params.dig(:service)
  hostGroup       = params.dig(:host_group)
  startTime       = params.dig(:start_time) || Time.now.to_i
  endTime         = params.dig(:end_time)
  author          = params.dig(:author)
  comment         = params.dig(:comment)
  type            = params.dig(:type)
  filter          = nil

  # sanitychecks
  #
  if( name == nil )

    return {
      :status  => 404,
      :message => 'missing downtime name'
    }
  end

  if( ['host','service'].include?(type.downcase) == false )

    return {
      :status  => 404,
      :message => "wrong downtype type. only 'host' or' service' allowed ('#{type}' giving"
    }
  else
    # we need the first char as Uppercase
    type.capitalize!
  end

  if( hostGroup != nil && hostName != nil )

    return {
      :status  => 404,
      :message => 'choose host or host_group, not both'
    }

  end

  if( hostName != nil )

    filter = sprintf( 'host.name=="%s"', hostName )
  elsif( hostGroup != nil )

    # check if hostgroup available ?
    #
    filter = sprintf( '"%s" in host.groups', hostGroup )
  else

    return {
      :status  => 404,
      :message => 'missing host or host_group for downtime'
    }
  end

  if( comment == nil )

    return {
      :status  => 404,
      :message => 'missing downtime comment'
    }
  end

  if( author == nil )

    return {
      :status  => 404,
      :message => 'missing downtime author'
    }
  else

    if( self.existsUser?( author ) == false )

      return {
        :status  => 404,
        :message => "these author ar not exists: #{author}"
      }
    end
  end


  if( endTime == nil )

    return {
      :status  => 404,
      :message => 'missing end_time'
    }
  else

    if( endTime.to_i <= startTime )

      return {
        :status  => 404,
        :message => 'end_time are equal or smaller then start_time'
      }
    end
  end

#       logger.debug( Time.at( startTime ).strftime( '%Y-%m-%d %H:%M:%S' ) )
#       logger.debug( Time.at( endTime ).strftime( '%Y-%m-%d %H:%M:%S' ) )

  payload = {
    "type"        => type,
    "start_time"  => startTime,
    "end_time"    => endTime,
    "author"      => author,
    "comment"     => comment,
    "fixed"       => true,
    "duration"    => 30,
    "filter"      => filter
  }

#       logger.debug( JSON.pretty_generate( payload ) )

  result = Network.post( {
    :host    => name,
    :url     => sprintf( '%s/v1/actions/schedule-downtime', @icingaApiUrlBase ),
    :headers => @headers,
    :options => @options,
    :payload => payload
  } )

  logger.debug( result.class.to_s )

  return JSON.pretty_generate( result )


  # schedule downtime for a host
  #  --data '{ "type": "Host", "filter": "host.name==\"api_dummy_host_1\"", ... }'

  # schedule downtime for all services of a host
  #  --data '{ "type": "Service", "filter": "host.name==\"api_dummy_host_1\"", ... }'

  # schedule downtime for all hosts and services in a hostgroup
  #  --data '{ "type": "Host", "filter": "\"api_dummy_hostgroup\" in host.groups", ... }'

  #  --data '{ "type": "Service", "filter": "\"api_dummy_hostgroup\" in host.groups)", ... }'



end

#listDowntimes(params = {}) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/icinga2/downtimes.rb', line 154

def listDowntimes( params = {} )

  name = params.dig(:name)

  result = Network.get( {
    :host     => name,
    :url      => sprintf( '%s/v1/objects/downtimes/%s', @icingaApiUrlBase, name ),
    :headers  => @headers,
    :options  => @options
  } )

  return JSON.pretty_generate( result )


end