Class: Zabbapi::WatchDog

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbapi.rb

Instance Method Summary collapse

Instance Method Details

#wdadd(hostname, name, email, activeif, ip) ⇒ Object



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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/zabbapi.rb', line 52

def wdadd(hostname , name , email , activeif , ip)
	@hostname = hostname
	@name = name
	@email = email
	@activeif = activeif
	@ip = ip
	passwd = [*('a'..'z'),*('0'..'9')].sample(12).join

	puts "Add host to zabbix..."
	zbx = ZabbixApi.connect(
		:url => 'https://' + $wd_host + '/api_jsonrpc.php',
		:user => $wd_admin_login ,
		:password => $wd_admin_pass
	)
	begin
		zbx.hostgroups.create(:name => @name)
	rescue
		puts "HostGroup exist!"
	else
		puts "HostGroup created!"
	end

	begin
		zbx.usergroups.get_or_create(:name => @name)
	rescue
		puts "UserGroup exist!"
	else
		zbx.query(
			:method => "usergroup.massadd",
			:params => {
				:usrgrpids => [zbx.usergroups.get_id(:name => @name)],
				:rights => [{
					:groupid => [zbx.usergroups.get_id(:name => @name)],
					:id => zbx.hostgroups.get_id(:name => $name) ,
					:permission => 2
					}]
			}
		)
		puts "UserGroup #{@name} added.."
	end

	begin
		zbx.users.create(
			:alias => $name,
			:type => 1,
			:passwd => passwd,
			:usrgrps => [zbx.usergroups.get_id(:name => @name)],
			:url => '/screens.php'
		)
	rescue
		puts "User exist!"
	else
		puts "User created!"
		puts " "
		puts "#Zabbix"
		puts 'https://' + $wd_host
		puts "#{@name} / #{passwd}"
		puts " "

		begin
			getuserid =  zbx.query(
				:method => "user.get",
				:params => {
					:filter => { :alias => ["#{@name}"] },
					:output => {
						:filter => "userid"
					}
				}

			)

			getuserid = Hash[*getuserid]
			$user_id_glob = getuserid["userid"]
			zbx.query(:method => "user.addmedia",
				:params => {
					:users => { :userid => getuserid["userid"]},
					:medias => [
					{
						:mediatypeid => "100100000000001" ,
						:sendto => @email,
						:active => 0,
						:severity => 48,
						:period => "1-7,00:00-24:00"
					}
					]
				}
			)
		rescue
			puts "UserMedia exist!"
		else
			puts "UserMedia added to #{@name}"
		end


	end

	if $host.chars.first == 'l'
		$tplid = '100100000010001'
	elsif $host.chars.first == 'f'
		$tplid = '100100000010008'
	else
		$tplid = '100100000010001'
	end


	begin
		zbx.query(:method => "host.create" ,
			:params => {
				:host => @hostname ,
				:interfaces => {
					:type => 1,
					:main => 1,
					:ip => $ip ,
					:dns => "",
					:useip => 1,
					:port => 10050
				},
				:groups => [{ :groupid => zbx.hostgroups.get_id(:name => @name) }],
				:templates => { :templateid => $tplid }
			}
		)
	rescue
		puts "Host exist or got error!"
		abort "please del #{@hostname} first."
	else
		puts "Added host #{@hostname} to #{@name} !"
	end

	begin
	zbx.query(
		:method => "action.create" ,
		:params => [ {
			:name => "all #{@name} triggs",
			:eventsource => 0 ,
			:status => 0 ,
			:evaltype => 0 ,
			:esc_period => 3600 ,
			:def_shortdata => '{TRIGGER.NAME}: {STATUS}' ,
			:def_longdata => '{TRIGGER.NAME}: {STATUS}',
			:usrgrpid => zbx.usergroups.get_id(:name => @name),
			:conditions => [
				{
					:conditiontype => 0 ,
					:operator => 0 ,
					:value => zbx.hostgroups.get_id(:name => $name)
				}
			],
			:operations => [
				{
					:operationtype => 0 ,
				:opmessage_grp => [
					{
						:usrgrpid => zbx.usergroups.get_id(:name => @name)
				}],
				:opmessage => {:default_msg => 1 }
			}
			]
		}]
	)
	rescue
		puts "Action exist!"
	end

	puts "Update host templates..."

	zbx.templates.mass_add(
		:hosts_id => [zbx.hosts.get_id(:host => @hostname)],
		:templates_id => [100100000010962 , 100100000010003 , 100100000010099 , 100100000011805]
	)
	if $host[2] == "i"
		zbx.templates.mass_add(
			:hosts_id => [zbx.hosts.get_id(:host => @hostname)],
			:templates_id => [200200000010062]
		)
	end
	puts "Add host to group..."

	zbx.query(:method => "hostgroup.massadd" , :params => {:groups => {:groupid => "100100000000614"} , :hosts => {:hostid => zbx.hosts.get_id(:host => @hostname)}} )
	begin
		puts "Update screen graphs..."
		ifaceknow = [ "eth0" , "eth1" , "em1" , "bond0" , "bond1" , "eth2" , "eth3" , "eth4" , "p4p1" ]
		puts "discovery active interface graph"
		sleep(66) unless ifaceknow.include? @activeif
	rescue
		abort "Its not error!!!! please wait 5 min , and update host , because new iface not discovered."
	end
	begin
		iface = "Traffic " + @activeif
		iface = "Traffic on " + @activeif unless ifaceknow.include? @activeif
		$graphtoscreen1 = zbx.graphs.get_ids_by_host(:host => @hostname , :filter => iface )
		$graphtoscreen2 = zbx.graphs.get_ids_by_host(:host => @hostname , :filter => "CPU\ Utilization")
		$graphtoscreen3 = zbx.graphs.get_ids_by_host(:host => @hostname , :filter => "Load\ Average")
		$graphtoscreen4 = zbx.graphs.get_ids_by_host(:host => @hostname , :filter => "MySQL\ queries")
		$graphtoscreen5 = zbx.graphs.get_ids_by_host(:host => @hostname , :filter => "Processes")
		$graphtoscreen6 = zbx.graphs.get_ids_by_host(:host => @hostname , :filter => "Memory")
		$graphtoscreen1 = $graphtoscreen1.map(&:to_i)
		$graphtoscreen2 = $graphtoscreen2.map(&:to_i)
		$graphtoscreen3 = $graphtoscreen3.map(&:to_i)
		$graphtoscreen4 = $graphtoscreen4.map(&:to_i)
		$graphtoscreen5 = $graphtoscreen5.map(&:to_i)
		$graphtoscreen6 = $graphtoscreen6.map(&:to_i)

		temaral = zbx.screens.get_or_create_for_host(
			:hosts_id => [zbx.hosts.get_id(:host => @hostname)],
			:screen_name => @hostname,
			:height => 180,
			:width => 360,
			:vsize => 3,
			:hsize => 2,
			:halign => 0,
			:valign => 0,
			:graphids => [ $graphtoscreen1[0] , $graphtoscreen2[0] , $graphtoscreen3[0] , $graphtoscreen4[0] , $graphtoscreen5[0] , $graphtoscreen6[0] ]
		)

	rescue
		iface = "Traffic " + @activeif
		$graphtoscreen1 = zbx.graphs.get_ids_by_host(:host => @hostname , :filter => iface )
		$graphtoscreen2 = zbx.graphs.get_ids_by_host(:host => @hostname , :filter => "CPU\ Utilization")
		$graphtoscreen3 = zbx.graphs.get_ids_by_host(:host => @hostname , :filter => "Load\ Average")
		$graphtoscreen4 = zbx.graphs.get_ids_by_host(:host => @hostname , :filter => "MySQL\ queries")
		$graphtoscreen1 = $graphtoscreen1.map(&:to_i)
		$graphtoscreen2 = $graphtoscreen2.map(&:to_i)
		$graphtoscreen3 = $graphtoscreen3.map(&:to_i)
		$graphtoscreen4 = $graphtoscreen4.map(&:to_i)
		temaral = zbx.screens.get_or_create_for_host(
			:hosts_id => [zbx.hosts.get_id(:host => @hostname)],
			:screen_name => @hostname,
			:height => 180,
			:width => 360,
			:vsize => 2,
			:hsize => 2,
			:halign => 0,
			:valign => 0,
			:graphids => [ $graphtoscreen1[0] , $graphtoscreen2[0] , $graphtoscreen3[0] , $graphtoscreen4[0] ]
		)
	end

	getuserid =  zbx.query(
           :method => "user.get",
           :params => {
           	:filter => { :alias => ["#{@name}"] },
           	:output => {
           		:filter => "userid"
               }
           }

       )
       getuserid = Hash[*getuserid]
       @user_id_glob = getuserid["userid"]
	triggers = zbx.query(
	  	:method => "screen.update",
	  	:params => {
	      	:screenid => "#{temaral}",
	      	:userid => @user_id_glob
	  	}
	)
	puts "Done!"
end

#wddel(hostname) ⇒ Object



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
# File 'lib/zabbapi.rb', line 24

def wddel(hostname)
		@hostname = hostname
		zbx = ZabbixApi.connect(
			:url => 'https://' + $wd_host + '/api_jsonrpc.php',
			:user => $wd_admin_login ,
			:password => $wd_admin_pass
		)
		begin
			zbx.hosts.delete zbx.hosts.get_id(:host => @hostname )
		rescue
			puts "unknown host with hostname " + @hostname
		else
			begin
				$idscreenfordel = zbx.screens.get(
					:name => @hostname
				)
				$idscreenfordel = Hash[*$idscreenfordel]
				zbx.screens.delete(
				$idscreenfordel["screenid"]
				)
			rescue
				puts "host with hostname " + @hostname + " deleted"
			else
				puts "host with hostname " + @hostname + " deleted"
			end
		end
end