Created
January 17, 2013 13:38
-
-
Save olibob/4555955 to your computer and use it in GitHub Desktop.
CMDB access
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Author: Olivier Robert | |
# Date: 12/07/2012 | |
# Version: 0.0.2 | |
# Usage: cmdb configuration_item | |
# Description: | |
# Query CMDB for specific configuration items | |
require 'optparse' | |
require 'net/http' | |
require 'uri' | |
require 'rexml/document' | |
include REXML | |
### Helper Functions | |
# Webservice Interrogation | |
def webServiceRequest(url) | |
# Escape white spaces in URL | |
escapedURL = URI.escape(url) | |
uri = URI.parse(escapedURL) | |
http = Net::HTTP.new(uri.host, uri.port) | |
request = Net::HTTP::Get.new(uri.request_uri) | |
return http.request(request) | |
end | |
# Show usage | |
def help | |
puts "Usage: cmdb [options] configuration_item" | |
puts "Help: cmdb -h" | |
exit | |
end | |
# Check if there is an argument after all command line options | |
# have been parsed | |
def lastArgumentCheck | |
if ARGV.empty? | |
help | |
end | |
end | |
# Ci class | |
class Ci | |
def initialize(xmldoc) | |
@xmldoc = xmldoc | |
end | |
def show | |
root = @xmldoc.root | |
#puts root.attributes["count"] | |
root.elements.each do |ci| | |
# decoration | |
puts "-"*18 + " CI " + "-"*18 | |
ci.elements.each do |elt| | |
eltName = elt.name | |
eltName.gsub!(/_/, " ") | |
eltName.capitalize! | |
eltValue = elt.text | |
#puts "#{eltName}:\t#{eltValue}" if eltValue | |
printf("%-40s: %-20s\n", eltName, eltValue) if eltValue | |
end | |
end | |
end | |
end | |
if __FILE__ == $0 | |
### CONSTANTS | |
# Base URL used for all webservice accesses | |
BASE_URL = "http://www.itotools.ep.parl.union.eu/cmdb/" | |
CI_ELEMENTS = ["name", | |
"family", | |
"class", | |
"environment", | |
"dns_name", | |
"main_contact", | |
"description", | |
"installation_date", | |
"model", | |
"manufacturer", | |
"serial_number", | |
"ep_number", | |
"location", | |
"floor_location", | |
"room_location", | |
"cabinet_location", | |
"shelf_location", | |
"slot_location", | |
"mac_address", | |
"ip_address", | |
"admin_console", | |
"admin_console_ip", | |
"security_zone", | |
"security_zone_loc", | |
"operating_system", | |
"security_patch_level", | |
"hardware_processor_type", | |
"hardware_processor_speed", | |
"hardware_processor_count", | |
"hardware_processor_core", | |
"hardware_memory_installed", | |
"hardware_disk_capacity", | |
"hardware_volume_partition", | |
"software_contact_internal_managers", | |
"software_contact_external_managers", | |
"software_contact_developers", | |
"software_app_server", | |
"software_client_arch", | |
"software_critical_in_session", | |
"software_critical_out_session", | |
"software_internet_access", | |
"software_internet_test_url", | |
"software_intranet_access", | |
"software_intranet_test_url", | |
"software_portfolio", | |
"software_rpo", | |
"software_rto" | |
] | |
MODE_TO_QUERY = { | |
:getci => "getCIs?", | |
:rootcause => "getRootCauseCIs?ci=", | |
:impacted => "getImpactedCIs?ci=", | |
:checkapp => "checkApplication?code=" | |
} | |
### COMMAND LINE OPTION PARSING | |
# Query string based on command line options This hash will hold all of the options parsed from the | |
# command-line by OptionParser. | |
options = {} | |
optparse = OptionParser.new do |opts| | |
# Set a banner, displayed at the top of the help screen. | |
opts.banner = "Usage: cmdb [options] configuration_item" | |
# Define the options, and what they do | |
options[:mode] = :getci | |
opts.on('-m', '--mode OPT', [:getci, :impacted, :rootcause, :checkapp], 'CMDB Interrogation mode') do |m| | |
options[:mode] = m | |
end | |
options[:select] = [] | |
opts.on('-S', '--select mac_address,...', Array, 'CI element selection') do |s| | |
options[:select] = s | |
end | |
options[:order] = [] | |
opts.on('-o', '--order name,...', Array, 'Presentation Order') do |s| | |
options[:order] = s | |
end | |
options[:filter] = [] | |
opts.on('-f', '--filter a,b,c', Array, 'Not implemented') do |s| | |
options[:filter] = s | |
end | |
options[:level] = "1..9" | |
opts.on('-l', '--level LVL', 'Depth Level (root cause)') do |l| | |
options[:level] = l | |
end | |
options[:search] = [] | |
opts.on('-s', '--search class="Solaris Zone",...', Array, 'Search criteria') do |s| | |
options[:search] = s | |
end | |
# This displays the help screen, all programs are | |
# assumed to have this option. | |
opts.on('-h', '--help', 'Display this screen') do | |
puts opts | |
puts "\nAvailable CI elements and search criteria:" | |
CI_ELEMENTS.each do |item| | |
puts " - #{item}" | |
end | |
puts "\nExamples\n" | |
puts "cmdb -m r -s class=Unix -S name,ip_address eicixzq133 -l 1,2 -o ip_address,name" | |
puts 'cmdb -m r -S dns_name -s class="Solaris Zone" refin2' | |
puts "cmdb -s name=refin2" | |
puts "cmdb -s name=refin2 -S software_contact_developers" | |
puts "cmdb -m checkapp RFN" | |
exit | |
end | |
end | |
# Parse the command-line. Remember there are two forms | |
# of the parse method. The 'parse' method simply parses | |
# ARGV, while the 'parse!' method parses ARGV and removesd | |
# any options found there, as well as any parameters for | |
# the options. What's left is the list of files to resize. | |
optparse.parse! | |
### QUERY STRING | |
queryString = MODE_TO_QUERY[options[:mode]] | |
# check if we are in query option mode or not (& to add in the query string) | |
# true for impacted and root by default | |
if options[:mode] != :getci | |
queryOptions = true | |
lastArgumentCheck | |
queryString += ARGV[0] | |
else | |
queryOptions = false | |
end | |
# a selection is requested | |
if !options[:select].empty? | |
queryString += '&' if queryOptions | |
queryString += 'select=' + options[:select].join(",") | |
queryOptions = true | |
end | |
# an specific order is requested | |
if !options[:order].empty? | |
queryString += '&' if queryOptions | |
queryString += 'order=' + options[:order].join(",") | |
queryOptions = true | |
end | |
# not implemented yet | |
# if !options[:filter].empty? | |
# end | |
# search options | |
if !options[:search].empty? | |
queryString += '&' if queryOptions | |
queryString += options[:search].join("&") | |
queryOptions = true | |
end | |
# root cause depth | |
if !options[:level].empty? | |
if options[:mode] != :rootcause | |
#puts "level switch works only with rootcause mode: ignored" | |
else | |
queryString += '&' if queryOptions | |
queryString += 'level=' + options[:level] | |
queryOptions = true | |
end | |
end | |
### FINAL URL | |
url = BASE_URL + queryString | |
### REQUEST | |
response = webServiceRequest(url) | |
code = response.code # => ex. 301 | |
if code === "200" | |
if MODE_TO_QUERY[options[:mode]] != "checkApplication?code=" | |
xmldoc = Document.new(response.body) # response.body => The body (HTML, XML, blob, whatever) | |
newCi = Ci.new(xmldoc) | |
newCi.show | |
else | |
puts response.body | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment