Class: PolZabbapi::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



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/zabbapi.rb', line 347

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_p + '/api_jsonrpc.php',
		:user => $wd_admin_login_p ,
		:password => $wd_admin_pass_p
	)
	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 "#WDPolus"
		puts 'https://' + $wd_host_p
		puts "#{@name} / #{passwd}"
		puts " " 

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

			)

			getuserid = Hash[*getuserid]

			zbx.query(:method => "user.addmedia",
				:params => {
	  				:users => { :userid => getuserid["userid"]},
	  				:medias => [
	   				{
	      					:mediatypeid => "1" ,
	      					: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_p = '10001'
	elsif $host.chars.first == 'f'
		$tplid_p = '10075'
	else
		$tplid_p = '10001'
	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_p }
			}
		)
	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 => [10104 , 10073]
	)

	puts "Add host to group..."
	
	#zbx.query(:method => "hostgroup.massadd" , :params => {:groups => {:groupid => "100100000000614"} , :hosts => {:hostid => zbx.hosts.get_id(:host => @hostname)}} )
	
	puts "Done!"
end

#wddel(hostname) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/zabbapi.rb', line 318

def wddel(hostname)

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