zcollective 0.0.14 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/bin/zcollective +35 -19
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a43468c3bdb39ac2bad1c415283ca2f179746330
4
- data.tar.gz: f3c693754e9cb7701dc35bff9d11160fb14283cb
3
+ metadata.gz: 08f265b096412fb3b523d050552aaeef9d8803d3
4
+ data.tar.gz: 1f571024229a1e8df68602ee74305dbe070174ca
5
5
  SHA512:
6
- metadata.gz: 390bcfafbc5d7289f777b61482cb6fcc7ca31b2e356b88d96212ff7ca353a9cfcda9730eb31e59d69526ec67f9a70b706f010d7af96c75aabe76e91f16cd89ec
7
- data.tar.gz: dfd833d3089049ee5789f46d1bf259e24e11f84dc217d7968987a840b52384863bae6022e59a08aad6689890e5f0bbf89e985c95754696d222e241c5a672cf8c
6
+ metadata.gz: 88ba7dbca7361865084fa580accbc803f2c804edfcd4363c046b116f166130227c39ff6892e156defcdb7cd95cf1db97372793957f14f5b955fe2e0928d6dc5f
7
+ data.tar.gz: 278143ee71691f1f428eb6b5ac0684fc5992c1d8cf98913ac4fbda439eee21c3a05ac3acc6352445b01ff53fd2f6bbd2eff264d510103849a34c7973d6f033a3
data/bin/zcollective CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- # Copyright (c) 2012, 2013, The Scale Factory Ltd.
3
+ # Copyright (c) 2012 - 2016, The Scale Factory Ltd.
4
4
  # All rights reserved.
5
5
  #
6
6
  # Redistribution and use in source and binary forms, with or without
@@ -104,6 +104,10 @@ optparse = OptionParser.new do |opts|
104
104
  options[:ignore_classes] = i
105
105
  end
106
106
 
107
+ options[:hostgroup_facts] = []
108
+ opts.on('-f', '--hostgroup-facts fact1,fact2', Array, 'Create hostgroups and link hosts for given facts.') do |f|
109
+ options[:hostgroup_facts] = f
110
+ end
107
111
 
108
112
  end
109
113
 
@@ -205,9 +209,9 @@ end
205
209
  nested_hash = lambda {|hash, key| hash[key] = Hash.new(&nested_hash)}
206
210
  hosts = Hash.new(&nested_hash)
207
211
 
208
- # An array of collectives to create hostgroups of:
212
+ # Arrays of collectives and facts to create hostgroups of:
209
213
  collectives = Array.new
210
-
214
+ facts = Array.new
211
215
 
212
216
  ############################################################################
213
217
  # Fetch list of zabbix groups
@@ -399,6 +403,15 @@ else
399
403
  hosts[ short_hostname ][:mcollective][:classes] = inventory[:data][:classes]
400
404
  hosts[ short_hostname ][:mcollective][:collectives] = inventory[:data][:collectives]
401
405
 
406
+ # Filter facts based on --hostgroup-facts argument, return a list of fact values
407
+ filtered_facts = inventory[:data][:facts].select{|f| options[:hostgroup_facts].include? f}.values
408
+
409
+ # Divide facts containing a comma
410
+ filtered_facts.map!{|v| v.to_s.split(',')}.flatten!
411
+
412
+ hosts[ short_hostname ][:mcollective][:facts] = filtered_facts
413
+
414
+ facts << filtered_facts
402
415
  collectives << hosts[ short_hostname ][:mcollective][:collectives]
403
416
 
404
417
  end
@@ -410,20 +423,22 @@ end
410
423
 
411
424
 
412
425
  ###########################################################################
413
- # Fetch list of zabbix groups & Create hosts groups of discoverred
414
- # collectives, to add hosts to later
426
+ # Fetch list of zabbix groups & Create hosts groups of discovered
427
+ # collectives and facts, to add hosts to later
415
428
 
429
+ log.debug("facts: #{facts.flatten.inspect}")
430
+ log.debug("uniq facts: #{facts.flatten.uniq.inspect}")
416
431
  log.debug("collectives: #{collectives.flatten.inspect}")
417
432
  log.debug("uniq collectives: #{collectives.flatten.uniq.inspect}")
418
433
 
419
- collectives_to_hostgroups = collectives.flatten.uniq
434
+ items_to_hostgroups = collectives.flatten.uniq + facts.flatten.uniq
420
435
 
421
- collectives_to_hostgroups.each do | collective |
436
+ items_to_hostgroups.each do | item |
422
437
 
423
438
  log.debug( "Fetching list of zabbix hostgroups" )
424
439
 
425
- collective_hostgroup_name = "#{collective}"
426
- collective_hostgroup_id = nil
440
+ item_hostgroup_name = "#{item}"
441
+ item_hostgroup_id = nil
427
442
 
428
443
  zabbix_client.request( 'hostgroup.get',
429
444
  'search' => '',
@@ -432,34 +447,34 @@ collectives_to_hostgroups.each do | collective |
432
447
 
433
448
  log.debug("\tName: #{hostgroup['name']} ID: #{hostgroup['groupid']}")
434
449
 
435
- if hostgroup['name'] == collective_hostgroup_name
436
- collective_hostgroup_id = hostgroup['groupid']
450
+ if hostgroup['name'] == item_hostgroup_name
451
+ item_hostgroup_id = hostgroup['groupid']
437
452
  end
438
453
 
439
454
  end
440
455
 
441
- if collective_hostgroup_id.nil?
456
+ if item_hostgroup_id.nil?
442
457
 
443
458
  if options[:noop]
444
459
 
445
- log.debug("No #{collective} hostgroup, but not creating as " <<
460
+ log.debug("No #{item} hostgroup, but not creating as " <<
446
461
  "we're in --noop mode")
447
462
  else
448
463
 
449
- log.debug("No #{collective} hostgroup: creating")
464
+ log.debug("No #{item} hostgroup: creating")
450
465
 
451
466
  resp = zabbix_client.request( 'hostgroup.create',
452
- 'name' => collective
467
+ 'name' => item
453
468
  )
454
469
 
455
- collective_hostgroup_id = resp['groupids'].first
470
+ item_hostgroup_id = resp['groupids'].first
456
471
 
457
472
  end
458
473
 
459
474
  end
460
475
 
461
- log.info("New hostgroup: #{collective}")
462
- log.info("New hostgroup's ID: #{collective_hostgroup_id}")
476
+ log.info("New hostgroup: #{item}")
477
+ log.info("New hostgroup's ID: #{item_hostgroup_id}")
463
478
 
464
479
  end
465
480
 
@@ -557,8 +572,9 @@ hosts.each do |host,data|
557
572
  host_group_hash = {}
558
573
 
559
574
  log.debug("\tName: #{hostgroup['name']} ID: #{hostgroup['groupid']}")
575
+ discovered_groups = data[:mcollective][:facts] + data[:mcollective][:collectives]
560
576
 
561
- if data[:mcollective][:collectives].include? hostgroup['name']
577
+ if discovered_groups.include? hostgroup['name']
562
578
  host_group_hash['groupid'] = hostgroup['groupid']
563
579
  groups_by_id << host_group_hash
564
580
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zcollective
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Topper