-
Notifications
You must be signed in to change notification settings - Fork 1
/
sysinfo_csvexport.rb
executable file
·138 lines (126 loc) · 3.57 KB
/
sysinfo_csvexport.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
51
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
#!/usr/bin/env ruby
# Copyright 2012 IDERA. All rights reserved.
#
# sysinfo_csvexport.rb is a utility to export system information from Uptime Cloud Monitor to csv.
#
# encoding: utf-8
require 'rubygems'
require "json/pure"
require 'csv'
require './lib/ver'
require './lib/exportoptions'
require './lib/api'
require './lib/filters'
$output_path = '.'
$APIKEY = ''
$outpath_setup = false
$verbose = false
$debug = false
$Max_Timeout = 120
def state_to_s _state
@str = nil
case _state
when 0
@str = 'unknown'
when 1
@str = 'ok'
when 2
@str = 'warn'
when 3
@str = 'critical'
else
@str = '???'
end
return @str
end
def sysinfo_to_csv(allsystems)
begin
if $debug == true
puts "At start of sysinfo_to_csv and output path is #{$output_path}\n"
end
if $outpath_setup == false
$outpath_setup = true
if $output_path != '.'
if Dir.exists?("#{$output_path}/") == false
if $verbose == true
print "Creating directory..."
if Dir.mkdir("#{$output_path}/", 0775) == -1
print "** FAILED ***\n"
return false
else
FileUtils.chmod(0775, "#{$output_path}/")
print "Success\n"
end
else
if Dir.mkdir("#{$output_path}/", 0775) == -1
print "FAILED to create directiory #{$output_path}/\n"
return false
else
FileUtils.chmod(0775, "#{$output_path}/")
end
end
else
FileUtils.chmod(0775, "#{$output_path}/")
end
end
end
fname = "#{$output_path}/allsystems.csv"
if $verbose == true
puts "Writing to #{fname}\n\n"
end
CSV.open(fname, 'wb') do |csv|
num_systems = allsystems.length
row0 = ['uuid', 'hidden', 'hostname', 'tags', 'OS', 'OS version', 'collector verison',
'Create Date UTC', 'Last Update UTC', 'Created at', 'Last updated', 'health index',
'summary state', 'uptime state', 'blocked state', 'load state', 'cpu state',
'memory state', 'filesystems state' ]
csv << row0
ctr = 0
while ctr < num_systems
row0.clear
sys = allsystems[ctr]
attr = sys['a']
os = nil
case attr['o']
when 'm'
os = 'Mac OS X'
when 'l'
os = 'Linux'
when 'w'
os = 'Microsoft Windows'
when 'f'
os = 'FreeBSD'
end
if os.nil?
puts "Unrecognized OS: #{attr['o']}\n"
os = "???"
end
row0 = [ sys['uuid'], sys['hid'], attr['n'], attr['t'], os, attr['ov'], attr['rv'],
Time.at(attr['c']).utc, Time.at(attr['p']).utc, attr['c'], attr['p'], attr['h'], state_to_s(attr['s']),
state_to_s(attr['us']), state_to_s(attr['bs']), state_to_s(attr['ls']), state_to_s(attr['cs']),
state_to_s(attr['ms']), state_to_s(attr['fs'])]
csv << row0
sys.clear
attr.clear
ctr = ctr + 1
end
end
return true
rescue Exception => e
puts "sysinfo_to_csv exception ... error is #{e.message}\n"
return false
end
end
options = ExportOptions.parse(ARGV, 'Usage: sysinfo_csvexport.rb APIKEY [options]', '')
puts $VersionString
if options != nil
tr = Time.now
trun = Time.new(tr.year,tr.month,tr.day,tr.hour,tr.min,tr.sec)
puts "Time and Date of this data export: #{trun}\n"
allsystems = GetSystems.all($APIKEY)
if allsystems != nil
sysinfo_to_csv(allsystems)
else
puts "No systems found\n"
end
end