-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsec.rb
executable file
·791 lines (680 loc) · 18.9 KB
/
parsec.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
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
310
311
312
313
314
315
316
317
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
346
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
#!/usr/bin/env ruby
# Name: parsec (Explorer Parser)
# Version: 2.7.4
# Release: 1
# License: CC-BA (Creative Commons By Attribution)
# http://creativecommons.org/licenses/by/4.0/legalcode
# Group: System
# Source: N/A
# URL: http://lateralblast.com.au/
# Distribution: UNIX
# Vendor: Lateral Blast
# Packager: Richard Spindler <richard@lateralblast.com.au>
# Description: Ruby script for processing explorer
# Load gems
require 'rubygems'
require 'pathname'
require 'fileutils'
require 'etc'
require 'date'
require 'csv'
def install_gem(gem_name)
puts "Information:\tInstalling #{gem_name}"
%x[gem install #{gem_name}]
Gem.clear_paths
return
end
def install_pkg(pkg_name)
$os_name = %[uname -a].chomp
if $os_name.match(/SunOS/) and $os_name.match(/5\.11/)
if pkg_name.match(/\@/)
pkg_name = pkg_name.split(/\@/)[0]
end
puts "Information:\tInstalling #{pkg_name}"
%x[pkg install #{pkg_name}]
else
if $os_name.match(/Darwin/)
if File.exist?("/usr/local/bin/brew")
puts "Information:\tInstalling #{pkg_name}"
%x[brew install #{pkg_name}]
end
end
end
return
end
begin
require 'iconv'
rescue LoadError
install_pkg("libiconv")
install_gem("iconv")
end
begin
require 'getopt/long'
rescue LoadError
install_gem("getopt")
end
begin
require 'fileutils'
rescue LoadError
install_gem("fileutils")
end
begin
require 'hex_string'
rescue LoadError
install_gem("hex_string")
end
begin
require 'terminal-table'
rescue LoadError
install_gem("terminal-table")
end
begin
require 'unpack'
rescue LoadError
install_gem("unpack")
end
begin
require 'enumerate'
rescue LoadError
install_gem("enumerate")
end
begin
require 'prawn'
rescue LoadError
install_gem("prawn")
end
begin
require 'prawn/table'
rescue LoadError
install_gem("prawn-table")
end
begin
require 'fastimage'
rescue LoadError
install_gem("fastimage")
end
begin
require 'nokogiri'
rescue LoadError
install_gem("nokogiri -- --use-system-libraries")
end
begin
require 'rmagick'
include Magick
rescue LoadError
install_pkg("imagemagick@6")
install_gem("rmagick")
require 'rmagick'
include Magick
end
# Script name
$script = $0
# Valid output formats
$valid_output_formats = [ 'table', 'pdf', 'pipe', 'csv', 'text' ]
# Handle CTRL-C more gracefully
Signal.trap("SIGINT") do
exit 130
end
# Defaults for PDF
$default_font_size = 12
$section_font_size = 28
$heading_font_size = 18
$table_font_size = 10
$company_name = "Lateral Blast Pty Ltd"
$author_name = "Richard Spindler"
# Defaults for output
$output_format = "pipe"
# Print usage
def print_help()
switches = []
long_switch = ""
short_switch = ""
help_info = ""
puts ""
puts "Usage: "+$script
puts ""
file_array = IO.readlines $0
option_list = file_array.grep(/\[ "--/)
option_list.each do |line|
if !line.match(/file_array/)
help_info = line.split(/# /)[1]
switches = line.split(/,/)
long_switch = switches[0].gsub(/\[/,"").gsub(/\s+/,"")
short_switch = switches[1].gsub(/\s+/,"")
if long_switch.gsub(/\s+/,"").length < 7
puts long_switch+",\t\t"+short_switch+"\t"+help_info
else
puts long_switch+",\t"+short_switch+"\t"+help_info
end
end
end
puts
return
end
# Get command line arguments
# Print help if given none
if !ARGV[0]
print_help()
exit
end
# Try to make sure we have valid long switches
ARGV[0..-1].each do |switch|
if switch.match(/^-[a-z]=/)
puts "Invalid command line option: "+switch
exit
end
end
# Load methods
if Dir.exist?("./methods")
file_list = Dir.entries("./methods")
for file in file_list
if file =~ /rb$/
require "./methods/#{file}"
end
end
end
# Report types
report = {}
report["aggr"] = "Report on aggregate information"
report["all"] = "Report on everything"
report["coreadm"] = "Report on coreadm information"
report["cpu"] = "Report on CPU information"
report["cron"] = "Report on cron information"
report["crypto"] = "Report on crypto information"
report["cups"] = "Report on CUPS information"
report["disk"] = "Report on disks"
report["dumpadm"] = "Report on dumpadm information"
report["eeprom"] = "Report on EEPROM"
report["elfsign"] = "Report on elfsign information"
report["explorer"] = "Report on Explore information"
report["firmware"] = "Report on Firmware information"
report["fru"] = "Report on FRU information"
report["fs"] = "Report on filesystem information"
report["host"] = "Report on host information"
report["inetadm"] = "Report on inetadm information"
report["inetd"] = "Report on inetd information"
report["inetinit"] = "Report on inetinit information"
report["io"] = "Report on all IO"
report["ipmi"] = "Report on IPMI information"
report["ldom"] = "Report on LDom information"
report["link"] = "Report on link information"
report["locale"] = "Report on locale information"
report["login"] = "Report on locale information"
report["lu"] = "Report on Live Upgrade information"
report["kernel"] = "Report on kernel information"
report["keyserv"] = "Report on keyserv information"
report["memory"] = "Report on Memory information"
report["modinfo"] = "Report on Kernel Module information"
report["module"] = "Report on Kernel Module information"
report["mount"] = "Report on mount information"
report["ndd"] = "Report on ndd information"
report["network"] = "Report on network information"
report["ntp"] = "Report on NTP information"
report["obp"] = "Report on OBP information"
report["os"] = "Report on OS information"
report["package"] = "Report on package information"
report["pam"] = "Report on PAM information"
report["password"] = "Report on password information"
report["patch"] = "Report on patch information"
report["power"] = "Report on power information"
report["security"] = "Report on Security information"
report["sendmail"] = "Report on Sendmail settings"
report["sensors"] = "Report on Sensor information"
report["serial"] = "Report on Chassis Serial information"
report["serials"] = "Report on Component Serial information"
report["services"] = "Report on Services information"
report["slots"] = "Report on Upgradeable slot information"
report["snmp"] = "Report on SNMP information"
report["ssh"] = "Report on SSH information"
report["su"] = "Report on SU information"
report["suspend"] = "Report on Suspend information"
report["syslog"] = "Report on syslog information"
report["system"] = "Report on system information"
report["swap"] = "Report on swap information"
report["svm"] = "Report on Solaris Volume Manager information"
report["tcp"] = "Report on TCP information"
report["telnet"] = "Report on Telnet information"
report["udp"] = "Report on UDP information"
report["vnic"] = "Report on VNIC information"
report["veritas"] = "Report on Veritas information"
report["zfs"] = "Report on ZFS information"
report["zone"] = "Report on Zone information"
#report[""]=""
# Options
# Process options
begin
option = Getopt::Long.getopts(
[ "--prefix", "-b", Getopt::REQUIRED ], # Set base/prefix directory
[ "--address", "-A", Getopt::REQUIRED ], # Set Partner Address
[ "--city", "-S", Getopt::REQUIRED ], # Set Partner City
[ "--customer", "-C", Getopt::REQUIRED ], # Set customer name
[ "--date", "-T", Getopt::REQUIRED ], # Set date (used in conjunction with list)
[ "--dir", "-d", Getopt::BOOLEAN ], # Specify directory where explorers are (default is ./explorers)
[ "--dodisks", "-D", Getopt::BOOLEAN ], # Do disks (reports information on all disks)
[ "--format", "-f", Getopt::REQUIRED ], # Output format
[ "--logo", "-L", Getopt::REQUIRED ], # Set Partner Logo
[ "--list", "-l", Getopt::BOOLEAN ], # List explorers (also lists dmidecodes and facters if present)
[ "--masked", "-m", Getopt::BOOLEAN ], # Mask hostnames, IPs, MAC addresses etc
[ "--nocheck", "-m", Getopt::BOOLEAN ], # Don't do security checks
[ "--model", "-M", Getopt::REQUIRED ], # Set model (used in conjunction with list)
[ "--input", "-i", Getopt::REQUIRED ], # Input file
[ "--output", "-o", Getopt::REQUIRED ], # Output file
[ "--partner", "-P", Getopt::REQUIRED ], # Set Partner Name
[ "--pause", "-p", Getopt::BOOLEAN ], # Pause between each report when running against all hosts (useful for debugging)
[ "--report", "-R", Getopt::REQUIRED ], # Report type (e.g. all, cpu, memory)
[ "--server", "-s", Getopt::REQUIRED ], # Server to run explorer report for
[ "--search", "-S", Getopt::REQUIRED ], # Search term (currently used with list to do some simple searching)
[ "--type", "-t", Getopt::REQUIRED ], # Type of report (default is explorer)
[ "--value", "-T", Getopt::REQUIRED ], # Value to search for (used in conjunction with search and list)
[ "--temp", "-w", Getopt::REQUIRED ], # Work directory
[ "--usage", "-u", Getopt::REQUIRED ], # Display usage information
[ "--use", "-U", Getopt::REQUIRED ], # Override defaults, e.g. use gzip rather than pgiz
[ "--year", "-Y", Getopt::REQUIRED ], # Set year(used in conjunction with list)
[ "--date", "-y", Getopt::REQUIRED ], # Set date (used in conjunction with list)
[ "--help", "-h", Getopt::BOOLEAN ], # Display help information
[ "--verbose", "-v", Getopt::BOOLEAN ], # Verbose output
[ "--version", "-V", Getopt::BOOLEAN ], # Display version
[ "--changelog", "-c", Getopt::BOOLEAN ] # Print changelog
)
rescue
print_help()
exit
end
# Print Changelog
def print_changelog()
if File.exist?("changelog")
changelog = File.readlines("changelog")
changelog = changelog.reverse
changelog.each_with_index do |line, index|
line = line.gsub(/^# /,"")
if line.match(/^[0-9]/)
puts line
puts changelog[index-1].gsub(/^# /,"")
puts
end
end
end
return
end
def check_output_format()
if !$valid_output_formats.grep(/#{$output_format}/)
puts "Invalid output format"
exit
end
end
opt = option
# Print examples
def print_examples(usage)
match = 0
if usage.match(/example|report|server|cpu/)
match = 1
puts "Run a CPU report against a specific host"
puts
puts "$ #{$script} --server=hostname --report=cpu"
puts
puts "Run a CPU report against all hosts"
puts
puts "$ #{$script} --server=all --report=cpu"
puts
end
if usage.match(/example|report|server|cpu/)
match = 1
puts "Run a all reports against a specific host"
puts
puts "$ #{$script} --server=hostname --report=all"
puts
puts "Run all reports against all hosts"
puts
puts "$ #{$script} --server=all --report=all"
puts
end
if usage.match(/example|report|server|cpu|format/)
match = 1
puts "Run all reports against a specific host and output a PDF report"
puts "(will create a file based on the hostname and report in the outputs directory)"
puts
puts "$ #{$script} --server=hostname --report=all --format=pdf"
puts
puts "Run all reports against a specific host and output PDF a report to a specific file"
puts
puts "$ #{$script} --server=hostname --report=all --format=pdf --output=hostename.pdf"
puts
end
if usage.match(/example|report|server|cpu|format/)
match = 1
puts "Run a all reports against a specific host and output it in a format that can be piped into another command"
puts
puts "$ #{$script} --server=hostname --report=all --format=pipe"
puts
end
if usage.match(/example|report|server|cpu|format/)
match = 1
puts "Run a all reports against a specific host and output it in a table format"
puts
puts "$ #{$script} --server=hostname --report=all --format=table"
puts
end
if match == 0
print_help()
end
return
end
# Print usage
def print_usage(usage,report)
case usage
when /reports/
puts
report.each do |type,info|
if type.length < 7
puts type+":\t\t"+info
else
puts type+":\t"+info
end
end
else
puts
print_examples(usage)
end
puts
return
end
# Set nocheck flag to ignore security checks
if option["nocheck"]
$nocheck = 1
else
$nocheck = 0
end
# Set logo for PDF report
if option["logo"]
$partner_logo = option["logo"]
if !File.exist?($partner_logo)
$partner_logo = ""
end
else
$partner_logo = ""
end
# Set partner for PDF report
if option["partner"]
$partner_name = option["partner"]
else
$partner_name = ""
end
# Set address for PDF report
if option["address"]
$partner_address = option["address"]
else
$partner_address = ""
end
# Set city for PDF report
if option["city"]
$partner_city = option["city"]
else
$partner_city = ""
end
# Overide defaults
if option["use"]
use_flag = option["use"]
case use_flag
when /star/
$tar_bin = %x[which star].chomp
if !$tar_bin.match(/star/) or $tar_bin.match(/no star/)
$tar_bin = %x[which tar].chomp
end
when /tar/
$tar_bin = %x[which tar].chomp
when /pigz/
$gzip_bin = %x[which pigz].chomp
if !$gzip_bin.match(/pigz/) or $gzip_bin.match(/no pigz/)
$gzip_bin = %x[which gzip].chomp
end
when /gzip/
$gzip_bin = %x[which gzip].chomp
end
end
# Print help
if option["help"]
print_help()
exit
end
if option["usage"]
usage = option["usage"]
print_usage(usage,report)
exit
end
# Enable verbose mode
if option["verbose"]
$verbose_mode = 1
puts "Operating in verbose mode"
end
# get server name
if option["server"]
host_name = option["server"].downcase
if !option["report"]
option["report"] = "all"
if $verbose_mode == 1
puts "reporting on all elements of "+host_name
end
end
end
# Get model type
if option["model"]
search_model = option["model"]
else
search_model = ""
end
# Get date string
if option["date"]
search_date = option["date"]
if !search_date.match(/latest|last|earliest|first/)
begin
search_date = Date.parse(search_date).to_s
rescue
puts "Invalid date"
exit
end
end
else
if option["list"]
search_date = ""
else
search_date = "latest"
end
end
# Get year string
if option["year"]
search_year = option["year"]
if search_year.length == 2
search_year = "20"+search_year
end
if search_year.match(/[a-z,A-Z]/)
puts "Invalid year"
exit
end
else
search_year = ""
end
# Check local config
if !option["help"] and !option["version"]
check_local_config()
end
if option["prefix"]
$base_dir = option["prefix"]
else
$base_dir = Dir.pwd
end
# Print version
if option["version"]
print_version()
exit
end
# Print changelog
if option["changelog"]
print_changelog()
exit
end
# Set work directory
if option["temp"]
$work_dir = option["temp"]
else
if !$work_dir.match(/[A-z]/)
$work_dir = "/tmp"
end
end
# Handle search term / value
if option["search"]
search_param = option["search"]
else
search_param = ""
end
# Handle search value
if option["value"]
search_value = option["value"]
else
search_value = ""
end
# Set format of report
if option["format"]
$output_format = option["format"].downcase
$output_format = $output_format.gsub(/textcsv|csv/,"pipecsv")
$output_format = $output_format.gsub(/text/,"pipe")
check_output_format()
else
$output_format = "pipe"
if $verbose_mode == 1
puts "Setting output type to "+$output_format
end
end
# Pause mode
if option["pause"]
$pause_mode = 1
else
$pause_mode = 0
end
# Mask data
if option["masked"]
$masked = 1
if $verbose_mode == 1
puts "Masking output"
end
end
# Set explorer directory
if option["dir"]
$exp_dir = opt["dir"]
else
if !$exp_dir.match(/[A-z]/)
$exp_dir = Dir.pwd+"/explorers"
end
end
# Set type
if option["type"]
input_type = option["type"]
else
input_type = "explorer"
end
# Set output file
if option["output"]
$output_file = option["output"]
$output_dir = File.dirname($output_file)
if !File.directory?($output_dir) and !File.symlink?($output_dir)
Dir.mkdir($output_dir)
end
if $verbose_mode == 1 and !host_name.match(/^all$/)
puts "Setting output file to: "+$output_file
end
if File.exist?($output_file)
File.delete($output_file)
FileUtils.touch($output_file)
else
FileUtils.touch($output_file)
end
else
$output_file = ""
$output_dir = $base_dir+"/output"
if !File.directory?($output_dir) and !File.symlink?($output_dir)
Dir.mkdir($output_dir)
end
if $output_format.match(/pdf/)
if !host_name.match(/^all$/)
$output_file = $output_dir+"/"+host_name+".txt"
end
end
end
# List explorers
if option["list"]
if input_type.match(/all|explorer/)
if !host_name
host_name = ""
end
list_explorers(search_model,search_date,search_year,host_name,search_param,search_value)
end
if input_type.match(/all|facter/)
list_facters()
end
if input_type.match(/all|dmidecode/)
list_dmidecodes()
end
exit
end
# Report on all disk information
if option["dodisks"]
$do_disks = 1
if $verbose_mode == 1
puts "Enabling full disk reporting"
end
end
# Handle filename and hostname options
if option["input"]
$exp_file = option["input"]
if !File.exist?($exp_file) and !File.symlink?($exp_file)
puts
puts "Input file: "+$exp_file+" does not exist"
exit
end
search_name = get_hostname_from_explorer_file($exp_file)
else
if !option["server"]
if !option["help"]
puts
puts "Input file or hostname not specified"
end
print_help()
exit
end
$exp_dir = $base_dir.chomp()
$exp_dir = $exp_dir+"/explorers"
search_name = option["server"]
end
# Get report type
if option["report"]
$report_type = option["report"]
$report_type = $report_type.gsub(/cpus/,"cpu").gsub(/zone$/,"zones").gsub(/disks/,"disk").gsub(/sds/,"svm").gsub(/filesystems/,"filesystem").gsub(/crypt$/,"crypto")
check_valid_report_type($report_type)
if !option["input"] and !option["server"]
puts "Input file or home name not specified"
print_help()
exit
end
end
# Get customer name
if option["customer"]
if $masked == "1"
customer_name = "Masked"
else
customer_name = option["customer"]
end
else
customer_name = ""
end
# Handle explorer output
if input_type.match(/explorer/)
if search_name.match(/\,/)
search_names = search_name.split(/\,/)
search_names.each do |search_name|
file_list = get_explorer_file_list(search_model,search_date,search_year,search_name,search_param,search_value)
handle_explorer(report,file_list,search_model,search_date,search_year,search_name)
end
else
file_list = get_explorer_file_list(search_model,search_date,search_year,search_name,search_param,search_value)
handle_explorer(report,file_list,search_model,search_date,search_year,search_name)
end
end