Class: ZabbixManager::Problems

Inherits:
Basic
  • Object
show all
Defined in:
lib/zabbix_manager/classes/problems.rb

Instance Method Summary collapse

Methods inherited from Basic

#add, #create, #create_or_update, #create_raw, #default_options, #delete, #delete_raw, #destroy, #get, #get_hostgroup_ids, #get_id, #get_or_create, #get_or_create_hostgroups, #get_raw, #hash_equals?, #initialize, #log, #merge_params, #mojo_update, #normalize_array, #normalize_hash, #parse_keys, #symbolize_keys, #update, #update_raw

Constructor Details

This class inherits a constructor from ZabbixManager::Basic

Instance Method Details

#ack_event(eventids) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/zabbix_manager/classes/problems.rb', line 121

def ack_event(eventids)
  # 请求后端
  @client.api_request(
    method: "event.acknowledge",
    params: {
      eventids: eventids,
      action: 2,
      message: "由 RUBY SCRIPT 自动关闭"
    }
  )
  # 返回运行结果
end

#allArray<Hash>

Get full/extended Zabbix data for Problem objects from API

Returns:

  • (Array<Hash>)

    Array of matching objects

Raises:

  • (ManagerError)

    Error returned when there is a problem with the Zabbix API call.

  • (HttpError)

    Error raised when HTTP status from Zabbix Server response is not a 200 OK.



99
100
101
# File 'lib/zabbix_manager/classes/problems.rb', line 99

def all
  get_full_data({})
end

#dump_by_id(data) ⇒ Hash

Dump Problem object data by key from Zabbix API

Parameters:

  • data (Hash)

    Should include desired object’s key and value

Returns:

  • (Hash)

Raises:

  • (ManagerError)

    Error returned when there is a problem with the Zabbix API call.

  • (HttpError)

    Error raised when HTTP status from Zabbix Server response is not a 200 OK.



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zabbix_manager/classes/problems.rb', line 41

def dump_by_id(data)
  log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"

  @client.api_request(
    method: "problem.get",
    params: {
      filter: {
        identify.to_sym => data[identify.to_sym]
      },
      output: "extend"
    }
  )
end

#get_full_data(data) ⇒ Hash

Get full/extended Problem data from Zabbix API

Parameters:

  • data (Hash)

    Should include object’s id field name (identify) and id value

Returns:

  • (Hash)

Raises:

  • (ManagerError)

    Error returned when there is a problem with the Zabbix API call.

  • (HttpError)

    Error raised when HTTP status from Zabbix Server response is not a 200 OK.



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
# File 'lib/zabbix_manager/classes/problems.rb', line 61

def get_full_data(data)
  log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"

  data = symbolize_keys(data)

  @client.api_request(
    method: "#{method_name}.get",
    params: {
      filter: {
        identify.to_sym => data[identify.to_sym]
      },
      eventids: data[:eventids] || nil,
      groupids: data[:groupids] || nil,
      hostids: data[:hostids] || nil,
      objectids: data[:objectids] || nil,
      applicationids: data[:applicationids] || nil,
      tags: data[:tags] || nil,
      time_from: data[:time_from] || nil,
      time_till: data[:time_till] || nil,
      eventid_from: data[:eventid_from] || nil,
      eventid_till: data[:eventid_till] || nil,
      recent: data[:recent] || false,
      sortfield: data[:sortfield] || ["eventid"],
      sortorder: data[:sortorder] || "DESC",
      countOutput: data[:countOutput] || nil,
      output: "extend",
      selectAcknowledges: "extend",
      selectTags: "extend",
      selectSuppressionData: "extend"
    }
  )
end

#identifyString

The id field name used for identifying specific Problem objects via Zabbix API

Returns:

  • (String)


15
16
17
# File 'lib/zabbix_manager/classes/problems.rb', line 15

def identify
  "name"
end

#keyString

The key field name used for Problem objects via Zabbix API However, Problem object does not have a unique identifier

Returns:

  • (String)


23
24
25
# File 'lib/zabbix_manager/classes/problems.rb', line 23

def key
  "problemid"
end

#keysString

Returns the object’s plural id field name (identify) based on key However, Problem object does not have a unique identifier

Returns:

  • (String)


31
32
33
# File 'lib/zabbix_manager/classes/problems.rb', line 31

def keys
  "problemids"
end

#method_nameString

The method name used for interacting with Hosts via Zabbix API

Returns:

  • (String)


8
9
10
# File 'lib/zabbix_manager/classes/problems.rb', line 8

def method_name
  "problem"
end

#remove_problemObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/zabbix_manager/classes/problems.rb', line 103

def remove_problem
  # 设置时间区间
  time_from = 180.days.ago.at_beginning_of_day.to_i
  time_till = 14.days.ago.at_beginning_of_day.to_i

  # 抓取制定区间的数据
  data      = get_full_data(time_from: time_from, time_till: time_till)
  event_ids = []

  # 收集所有的 eventid
  data.each do |item|
    event_ids << item["eventid"]
  end

  # 返回 event_ids
  ack_event event_ids
end