forked from cgarrigues/Magento-Healthcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealthcheck
executable file
·1480 lines (1332 loc) · 46.7 KB
/
healthcheck
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
# coding: utf-8
require 'rexml/document'
require 'socket'
require 'optparse'
require 'pathname'
require 'resolv'
$options = Hash.new
$saved = Hash.new
$options[:roots] = []
OptionParser.new do |opt|
opt.on('-v', '--verbose', 'Show all output of commands') { |o| $options[:verbose] = o}
opt.on('-q', '--quiet', "Don't show output of unimportant stuff") { |o| $options[:quiet] = o}
opt.on('-x', '--findroots', "Find Mangento root directories that aren't at the webserver root") { |o| $options[:findroots] = o}
opt.on('-r', '--root dir', "Magento Root") { |o| $options[:roots].push o}
end.parse!
Effects = {
:off => 0,
:bright => 1,
:underline => 4,
:blink => 5,
:invert => 7,
:hide => 8,
:blackfg => 30,
:redfg => 31,
:greenfg => 32,
:yellowfg => 33,
:bluefg => 34,
:magentafg => 35,
:cyanfg => 36,
:whitefg => 37,
:defaultfg => 39,
:blackbg => 40,
:redbg => 41,
:greenbg => 42,
:yellowbg => 43,
:bluebg => 44,
:magentabg => 45,
:cyanbg => 46,
:whitebg => 47,
:defaultbg => 49,
}
def cleareol
return "[47;30m[0K"
end
def colorize(key)
effect = Effects[key]
return "[#{effect}m"
end
class Scannable
attr_accessor :heading
@@matches = Hash.new { |hash, key| hash[key] = Hash.new }
class << self
def matches
@@matches[self]
end
def matches= (newmatches)
newmatches.each do |key, value|
@@matches[self][key] = value
end
end
end
def initialize(*args)
end
def descriptor
self.class.superclass
end
def header
puts
puts "#{colorize(:underline)}#{self.descriptor}: #{colorize(:bright)}#{label}#{colorize(:off)}"
end
def printheader
if $options[:quiet]
@printedheader = false
else
header
@printedheader = true
end
end
def printline(line)
unless @printedheader
header
@printedheader = true
end
if self.heading
puts self.heading
self.heading = nil
end
puts line
end
def okay(data, test, value)
data = cleanup(data)
if test == :save
$saved[value] = data
return :note
elsif test == :count
if $saved[value]
$saved[value] += 1
else
$saved[value] = 1
end
elsif test == :collect
unless $saved[value]
$saved[value] = []
end
$saved[value].push data
else
if value.class == Symbol
value = Integer $saved[value]
end
if test == "="
return data == value
elsif test == ">"
return data > value
elsif test == "<"
return data < value
elsif test == ">="
return data >= value
elsif test == "<="
return data <= value
elsif test == "eq"
return data == value
elsif test == "ne"
return data != value
elsif test == :okay
return true
elsif test == :alsocheck
alsoscan [value, data]
return :note
else
return false
end
end
end
def cleanup(value)
if %r(^\d+\.\d+$) =~ value
value = Float value
elsif %r(^\d+$) =~ value
value = Integer value
end
return(value)
end
def scanline(line)
match = false
showline = false
self.class.matches.each do |key, tests|
if key.is_a? Array
(field, value, key) = key
unless ($saved[field] == value)
key = nil
end
end
if key and (matchdata = (key.match line))
match = true
if tests.is_a? Array
(1..tests.length).to_a.reverse.each do |i|
testpairs = tests[i-1].dup
endinsert = nil
state = nil
note = nil
testpairs.each do |test, value|
if test == :note
note = value
else
setstate = okay(matchdata[i], test, value)
if setstate == :note
unless state
endinsert = colorize(:off) + colorize(:bright)
state = :note
end
elsif setstate
unless state == :bad
endinsert = colorize(:off) + colorize(:bright)
state ||= :good
end
else
if test == :flag
if value
endinsert = colorize(:off) + colorize(:invert) + " #{value} " + colorize(:off) + colorize(:bright)
else
endinsert = colorize(:off) + colorize(:bright)
end
else
if value.is_a? Symbol
endinsert = colorize(:off) + colorize(:invert) + " (should be #{test} #{$saved[value]} (#{value})) " + colorize(:off) + colorize(:bright)
else
endinsert = colorize(:off) + colorize(:invert) + " (should be #{test} #{value}) " + colorize(:off) + colorize(:bright)
end
end
state = :bad
end
end
end
if state == :bad
begininsert = colorize(:redbg)
showline = true
if note and not $notes.member?(note)
$notes.push note
end
elsif state == :good
begininsert = colorize(:greenfg)
showline = false
else
begininsert = colorize(:yellowfg)
end
line.insert(matchdata.end(i), endinsert)
line.insert(matchdata.begin(i), begininsert)
end
line = (colorize(:bright) + line + colorize(:off))
elsif tests == :heading
unless $options[:verbose]
self.heading = line
showline = false
match = false
end
end
end
end
if (match or $options[:verbose])
unless $options[:quiet] and not showline
return line
end
end
end
def scan
$notes = []
printheader
openstream do |s|
while (line = s.gets)
if (line = scanline line.chop)
printline line
end
end
end
if $? and $?.exitstatus > 0
printline colorize(:redbg) + "Shell return code #{$?.inspect}" + cleareol + colorize(:off)
end
if self.class.matches == {}
printline colorize(:invert) + 'No match rules defined yet' + cleareol + colorize(:off)
end
$notes.each do |note|
puts cleareol + note
end
print colorize(:off) + "[0K"
end
end
class ShellCommand < Scannable
attr_reader :command
@@cmd = Hash.new
class << self
def cmd
@@cmd[self]
end
def cmd= (cmd)
@@cmd[self] = cmd
end
end
def cmd
@@cmd[self.class]
end
def label
cmd
end
def openstream
open "|#{cmd} 2>&1" do |s|
yield s
end
end
end
class Cache < ShellCommand
attr_reader :ipaddress
attr_reader :port
def initialize(ipaddress, port)
@ipaddress = ipaddress
@port = port
end
def quitcmd
"quit"
end
def label
if %r(^[\d\.]+$) =~ ipaddress
"#{self.class} at #{ipaddress}:#{port}"
else
"#{self.class} at #{ipaddress}"
end
end
def opentcporunixsocket(ipaddress, port)
if %r(^[\d\.]+$) =~ ipaddress
return TCPSocket.open(ipaddress, port)
else
return UNIXSocket.open(ipaddress)
end
end
def openstream
begin
opentcporunixsocket(ipaddress, port) do |s|
s.puts cmd + "\r"
s.puts quitcmd + "\r"
yield s
end
rescue Exception => e
printline colorize(:redbg) + e.message + colorize(:off)
end
end
end
class Memcached < Cache
self.cmd = "stats"
self.matches = {
%r(STAT evictions (\d+)) => [{"=" => 0}],
}
end
class Redis < Cache
self.cmd = 'info'
self.matches = {
%r(redis_version:(\s+)) => [{:save => :redis_version}],
%r(evicted_keys:(\d+)) => [{"=" => 0}],
%r(connected_clients:(\d+)) => [{">" => 0}],
}
def initialize(ipandport)
(ipaddress, port) = ipandport.split(':')
if port
super(ipaddress, port)
else
super(ipandport, "")
end
if $saved[:network_addrs] and $saved[:network_addrs].include? ipaddress
puts colorize(:invert) + "Redis is on local host. Up to 50% performance boost if switched to Unix sockets." + colorize(:off)
end
Sysctl.matches = {
%r(vm.overcommit_memory = (\d+)) => [{"=" => 1,
:note => "Without overcommit_memory set, redis background save may fail under low memory conditions."}],
}
end
end
class Sysctl < ShellCommand
self.cmd = 'sysctl -a'
self.matches = {
%r(vm.swappiness = (\d+)) => [{"=" => 0,
:note => "The kernel will swap only to avoid an out of memory condition, when free memory will be below vm.min_free_kbytes limit."}],
%r(net.ipv4.tcp_tw_recycle = (\d+)) => [{"=" => 1,
:note => "tcp_tw_recycle = 1 will minimze excess processes in TIME_WAIT or SYN_RECV."}],
%r(net.ipv4.tcp_tw_reuse = (\d+)) => [{"=" => 1,
:note => "tcp_tw_reuse = 1 will minimze excess processes in TIME_WAIT or SYN_RECV."}],
%r(net.ipv4.tcp_fin_timeout = (\d+)) => [{"<=" => 10,
:note => "a low tcp_fin_timeout will minimze excess processes in TIME_WAIT or SYN_RECV."}],
}
end
class Netstat < ShellCommand
self.cmd = 'netstat -nap'
self.matches = {
%r(^Proto) => :heading,
%r((TIME_WAIT)) => [{:count => :time_wait}],
%r((SYN_RECV)) => [{:count => :syn_recv}],
}
def process_saved
if $saved[:time_wait]
if $saved[:time_wait] < 100
printline colorize(:greenfg) + "there are #{$saved[:time_wait]} connections stuck in TIME_WAIT" + colorize(:off)
else
printline colorize(:redbg) + "there are #{$saved[:time_wait]} connections stuck in TIME_WAIT" + colorize(:off)
end
end
if $saved[:syn_recv]
if $saved[:syn_recv] < 100
printline colorize(:greenfg) + "there are #{$saved[:syn_recv]} connections stuck in SYN_RECV" + colorize(:off)
else
printline colorize(:redbg) + "there are #{$saved[:syn_recv]} connections stuck in SYN_RECV" + colorize(:off)
end
end
end
end
class Iostat < ShellCommand
self.cmd = 'iostat -dx 3 6'
self.matches = {
%r(^Device) => :heading,
%r(^\S+\s+[\d\.]+\s+[\d\.]+\s+[\d\.]+\s+[\d\.]+\s+[\d\.]+\s+[\d\.]+\s+[\d\.]+\s+[\d\.]+\s+([\d\.]+)\s+[\d\.]+\s+[\d\.]+) => [
{"<" => 100},
]
}
end
class ScannableFile < Scannable
attr_reader :filename
def initialize(filename)
if %r(^/) =~ filename
@filename = filename
else
@filename = basedir + '/' + filename
end
end
def descriptor
self.class
end
def label
filename
end
def openstream
if filename =~ %r(\*)
Dir[filename].each do |f|
alsoscan [self.class, f]
end
elsif Pathname.new(filename).directory?
Dir.entries(filename).each do |f|
unless f =~ %r(^\.)
alsoscan [self.class, filename + "/" + f]
end
end
else
begin
open(filename) do |s|
yield s
end
rescue Exception => e
printline colorize(:redbg) + e.message + colorize(:off)
end
end
end
end
class CpuInfo < ScannableFile
def initialize(*args)
@filename = "/proc/cpuinfo"
end
self.matches = {
%r(^(processor)) => [{:count => :processorcount}],
}
end
class SelinuxConf < ScannableFile
def initialize(*args)
@filename = "/etc/selinux/config"
end
self.matches = {
%r(^SELINUX=(\w+)) => [{"ne" => "enforcing"}],
}
end
class Dmesg < ShellCommand
self.cmd = 'dmesg'
self.matches = {
%r(^(SELinux): Initializing.$) => [{:alsocheck => SelinuxConf,
:note => "SELinux being enabled and not properly configured may break many things."}],
%r((segfault)) => [{:flag => nil,
:note => "Any segfaulting process should be understood."}],
%r((\w+) invoked oom-killer) => [{:flag => "Ran out of memory",
:note => "Running out of memory can cause performance and intermittent problems"}],
%r((Out of memory:.*)) => [{:flag => "Ran out of memory",
:note => "Running out of memory can cause performance and intermittent problems"}],
}
end
class Vmstat < ShellCommand
self.cmd = 'vmstat 1 10'
self.matches = {
%r(\sr\s+b\s) => :heading,
# r b swpd free buff cache si so
%r(^\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+(\d+)\s+(\d+)) => [{"=" => 0,
:note => "You are swapping. Do you need more memory?"},
{"=" => 0,
:note => "You are swapping. Do you need more memory?"}],
}
end
class Mpmstat < ShellCommand
self.cmd = 'mpstat -P ALL'
end
class Free < ShellCommand
self.cmd = 'free -mo'
self.matches = {
%r(total) => :heading,
%r(Swap:\s+\d+\s+(\d+)) => [{"=" => 0,
:note => "Swap non-zero indicates that swapping has occured in the past.\n If this is small, it may not be an issue;\n if it's large, then something nasty happened sometime since the last reboot."}],
}
end
class Uptime < ShellCommand
self.cmd = 'uptime'
self.matches = {
%r(load average: ([\d\.]+), ([\d\.]+), ([\d\.]+)) => [{"<" => 100,
:note => "The load average is unually high."},
{"<" => 100,
:note => "The load average is unually high."},
{"<" => 100,
:note => "The load average is unually high."}],
}
end
class Last < ShellCommand
self.cmd = 'last'
self.matches = {
%r(^reboot.*(\w\w\w \w\w\w +\d+ \d\d:\d\d - \d\d\:\d\d)) => [{:okay => nil}],
}
end
class Ethtool < ShellCommand
def initialize(interface)
super()
self.class.cmd = "ethtool #{interface}"
end
self.matches = {
%r(Link detected: (\w+)) => [{"=" => "yes"}],
}
end
class Ifconfig < ShellCommand
self.cmd = 'ifconfig -a'
self.matches = {
%r(^(\w+)\s) => [{:alsocheck => Ethtool}],
%r(inet (?:addr:)?(\S+)\s) => [{:collect => :network_addrs}],
%r(inet6 (?:addr:)?\s*([0-9a-f\:]+)) => [{:collect => :network_addrs}],
%r(errors:(\d+) dropped:(\d+) overruns:(\d+) frame:(\d+)) => [{"=" => 0,
:note => "Network errors may indicate physical issues."},
{"=" => 0,
:note => "Dropped packets may indicate physical issues."},
{"=" => 0,
:note => "Network overruns may indicate physical issues."},
{"=" => 0,
:note => "Framing errors may indicate physical issues."}],
%r(errors:(\d+) dropped:(\d+) overruns:(\d+) carrier:(\d+)) => [{"=" => 0,
:note => "Network errors may indicate physical issues."},
{"=" => 0,
:note => "Dropped packets may indicate physical issues."},
{"=" => 0,
:note => "Network overruns may indicate physical issues."},
{"=" => 0,
:note => "Framing errors may indicate physical issues."}],
}
end
class Iptables < ShellCommand
self.cmd = 'iptables -L -n -v'
self.matches = {
%r(^Chain) => :heading,
}
end
class Lsmod < ShellCommand
self.cmd = 'lsmod'
self.matches = {
%r(^(ip_tables)) => [{:alsocheck => Iptables}],
}
end
class Collection
attr_reader :args
attr_accessor :children
attr_accessor :heading
def initialize(*args)
@args = args
end
def scan
end
def also_check
if children
return children.map {|child| [child, *args]}
else
return []
end
end
end
class Apache < Collection
def initialize(*args)
super
@children = [Httpdt, HttpdV, HttpdS, HttpdM]
end
end
class ScannableLog < ScannableFile
# This isn't necessary in Ruby 2.x
def realpath(filename)
if filename == (Pathname.new '/')
Pathname.new filename
elsif File.symlink? filename
Pathname.new File.readlink filename
else
dirname = Pathname.new File.dirname filename
basename = Pathname.new File.basename filename
linkpath = realpath(dirname)
if %r(^/) =~ linkpath.to_s
linkpath + basename
else
dirname.dirname + linkpath + basename
end
end
end
def logisrotated?(filename)
if $saved[:rotatedlogs]
$saved[:rotatedlogs].any? do |logpat|
File.fnmatch? realpath(logpat), realpath(filename), File::FNM_PATHNAME
end
end
end
def scan
super
unless logisrotated? filename
printline colorize(:redbg) + "#{filename} is not being rotated by logrotate" + cleareol + colorize(:off)
end
end
end
class PhpFpmErrorLog < ScannableLog
def basedir
"/opt/php7/usr"
end
self.matches = {
%r((seems busy).*) => [{:flag => "Do this"}],
%r((server reached pm.max_children setting).*) => [{:flag => "Do this"}],
%r(script '(.*)' \(request: ".*"\) executing too slow) => [{:flag => "Investigate this"}],
%r(ERROR: (.*)$) => [{:flag => nil}],
%r((exited on signal 11 \(SIGSEGV\)) after .* seconds from start) => [{:flag => "SEGVs are not good"}],
%r(said into stderr: "(.*)") => [{:flag => "Investigate this"}],
}
end
class PhpFpmSlowLog < ScannableLog
end
class PhpFpmConf < ScannableFile
self.matches = {
%r(^\s*include\s*=\s*(\S+)) => [{:alsocheck => PhpFpmConf}],
%r(^\s*error_log\s*=\s*(\S+)) => [{:alsocheck => PhpFpmErrorLog}],
%r(^\s*slowlog\s*=\s*(\S+)) => [{:alsocheck => PhpFpmSlowLog}],
%r(^\s*emergency_restart_interval\s*=\s*(\S+)) => [{:save => :emergencyrestartinterval}],
%r(^\s*emergency_restart_threshold\s*=\s*(\S+)) => [{:save => :emergencyrestartthreshold}],
%r(^\s*process_control_timeout\s*=\s*(\S+)) => [{:save => :processcontroltimeout}],
}
def process_saved
# unless $saved[:emergencyrestartinterval]
# printline colorize(:invert) + "set emergency_restart_interval" + colorize(:off)
# end
# unless $saved[:emergencyrestartthreshold]
# printline colorize(:invert) + "set emergency_restart_threshold" + colorize(:off)
# end
# unless $saved[:processcontroltimeout]
# printline colorize(:invert) + "set process_control_timeout" + colorize(:off)
# end
end
end
class RedisConf < ScannableFile
self.matches = {
%r(^\s*(maxmemory)) => [{:flag => "Don't set maxmemory"}],
}
end
class UnameA < ShellCommand
self.cmd = 'uname -a'
self.matches = {
%r(\s\S+\s(\S+)) => [{:save => :kernel_version}]
}
end
class DfHt < ShellCommand
self.cmd = 'df -hT'
self.matches = {
%r(\s(\d+)\%) => [{"<" => 75}],
%r(^(\S+):) => [{:flag => "NFS Mount",
:note => "An NFS mounted filesystem isn't necessarily an issue,\n but if magento is running from this partition, make certain it's performant."}],
}
end
class DfHit < ShellCommand
self.cmd = 'df -hiT'
self.matches = {
%r(\s(\d+)\%) => [{"<" => 75}],
}
end
class DocumentRoot < Scannable
attr_reader :rootpath
def initialize(rootpath)
@rootpath = Pathname.new rootpath
end
def descriptor
self.class
end
def label
rootpath
end
def scanroot(rootpath)
etcpath = rootpath + 'app/etc'
unless $saved[:magento_roots]
$saved[:magento_roots] = []
end
$saved[:magento_roots].push rootpath
["local.xml", "enterprise.xml"].each do |xmlfile|
if (etcpath + xmlfile).exist?
alsoscan [MagentoXmlFile, etcpath + xmlfile]
end
end
if (rootpath + 'app/Mage.php').exist?
alsoscan [MagentoVersion, rootpath]
end
if (logpath = rootpath + 'var/log/exception.log').exist?
alsoscan [MagentoExceptionLog, logpath]
end
if (htaccesspath = rootpath + '.htaccess').exist?
alsoscan [HtAccess, htaccesspath]
end
end
def findallrootpaths(rootpath)
rootpaths = []
if rootpath.exist?
etcpath = rootpath + "app/etc"
if etcpath.exist?
rootpaths.push rootpath
else
begin
Dir.entries(rootpath).grep(%r(^[^\.])) {|f| Pathname.new f}.select {|file| File.directory? rootpath + file}.each do |dir|
etcpath = rootpath + dir + "app/etc"
if etcpath.exist?
rootpaths.push(rootpath + dir)
else
rootpaths.concat findallrootpaths(rootpath + dir)
end
end
rescue Exception => e
printline colorize(:redbg) + e.message + colorize(:off)
end
end
end
return(rootpaths)
end
def scan
printheader
if rootpath.exist?
etcpath = rootpath + "app/etc"
if etcpath.exist?
scanroot rootpath
else
printline colorize(:redbg) + rootpath + " is not a magento root." + cleareol + colorize(:off)
if $options[:findroots]
findallrootpaths(rootpath).each do |dir|
printline colorize(:invert) + "But #{dir} is." + cleareol + colorize(:off)
scanroot rootpath + dir
end
end
end
else
printline colorize(:redbg) + rootpath + " does not exist" + colorize(:off)
end
end
end
class WebUser < Collection
attr_reader :userid
def initialize(userid)
@userid = userid
end
def scan
# crondir = Pathname.new "/var/spool/cron/"
# if (cronfile = crondir + userid).exist?
# alsoscan [UserCrontab, cronfile]
# end
end
end
class NginxLog < ScannableLog
def basedir
$saved[:nginxroot]
end
self.matches = {
}
end
class NginxErrorLog < ScannableLog
def basedir
$saved[:nginxroot] || '/'
end
self.matches = {
%r(\[error\].*\] (.*)$) => [{:flag => nil,
:note => "Investigate this error."}],
}
end
class NginxConf < ScannableFile
def basedir
$saved[:nginxroot] || '/'
end
self.matches = {
%r(^\s*include\s+(\S+);) => [{:alsocheck => NginxConf}],
%r(^\s*worker_processes\s+(\d+)) => [{"=" => :processorcount}],
%r(^\s*user\s+(\d+)) => [{:alsocheck => WebUser}],
%r(^\s*root\s+([/a-z0-9_\.\*-]+)) => [{:alsocheck => DocumentRoot}],
%r(^\s*access_log\s+([/a-z0-9_\.\*-]+)) => [{:alsocheck => NginxLog}],
%r(^\s*error_log\s+([/a-z0-9_\.\*-]+)) => [{:alsocheck => NginxErrorLog}],
%r(^\s*server_tokens\s+(\w+)) => [{"=" => "off",
:note => "turn off server_tokens for security reasons"}],
}
end
class MysqlLog < ScannableLog
self.matches = {
%r(^(\d+\s+\d+:\d+:\d+).*(error))i => [{:flag => "YYMMDD HH:MM:SS"},
{:flag => nil}],
%r(^(\d+\s+\d+:\d+:\d+).*(Database was not shut down normally)) => [{:flag => "YYMMDD HH:MM:SS"},
{:flag => nil}],
}
end
class SysCrontab < ScannableFile
self.matches = {
%r(^[^\#]+(/cron\.sh)) => [{:count => :cron}],
%r(^[^\#]+(/cron\.php)) => [{:flag => "should call cron.sh, not cron.php"}],
}
end
class UserCrontab < ScannableFile
self.matches = {
%r(^[^\#]+(/cron\.sh)) => [{:count => :cron}],
%r(^[^\#]+(/cron\.php)) => [{:flag => "should call cron.sh, not cron.php"}],
}
end
class SysCrontabs < Collection
def scan
alsoscan [SysCrontab, '/etc/crontab']
alsoscan [SysCrontab, '/etc/cron.d/*']
if Pathname.new('/var/spool/cron/crontabs').directory?
alsoscan [UserCrontab, '/var/spool/cron/crontabs/*']
else
alsoscan [UserCrontab, '/var/spool/cron/*']
end
end
end
class NginxV < ShellCommand
def initialize(command)
super()
self.class.cmd = "#{command} -V"
end
self.matches = {
%r(--prefix=([/a-z\.]+)) => [{:save => :nginxroot}],
%r(--conf-path=([/a-z\.]+)) => [{:alsocheck => NginxConf}],
%r(--error-log-path=([/a-z\.]+)) => [{:alsocheck => NginxErrorLog}],
%r(--http-log-path=([/a-z\.]+)) => [{:alsocheck => NginxLog}],
}
end
class NginxExe < Collection
def initialize(*args)
super
@children = [NginxV]
end
end
class PhpV < ShellCommand
self.cmd = 'php -v'
self.matches = {
%r(PHP (\d+\.\d+)) => [{"ne" => "5.7"}],
}
end
class PhpM < ShellCommand
self.cmd = 'php -m'
self.matches = {
%r(^(bcmath)$) => [{:save => :bcmath}],
%r(^(memcache)$) => [{:save => :cache}],
%r(^(redis)$) => [{:save => :cache}],
}
def process_saved
unless $saved[:bcmath]
printline colorize(:invert) + "bcmath is a required php module" + colorize(:off)
end
unless $saved[:cache]
printline colorize(:invert) + "either memcache or redis modules are required" + colorize(:off)
end
end
end
class PhpI < ShellCommand
self.cmd = 'php -i'
self.matches = {
%r(^([a-z]+)$)i => [{:save => :section}],
[:section, "session", %r(^session.gc_maxlifetime => (\d+) => (\d+))] => [{"=" => 1440,
:note => "a large session.gc_lifetime could leave sessions around"},
{"=" => 1440}],
[:section, "session", %r(^session.gc_probability => (\d+) => (\d+))] => [{"=" => 1},
{"=" => 1}],
[:section, "session", %r(^session.cookie_lifetime => (\d+) => (\d+))] => [{"=" => 0},
{"=" => 0}],
[:section, "Core", %r(^PHP Version => (\S+))] => [{:save => :php_version}],
[:section, "Core", %r(^memory_limit => (\d+)M => (\d+)M)] => [{">=" => 256},
{">=" => 256}],
[:section, "Core", %r(^max_execution_time => (\d+) => (\d+))] => [{"=" => 18000,
:note => "max_execution_time should be set very high to assure that indexing completes."},
{"=" => 18000}],
[:section, "Core", %r(^max_input_vars => (\d+) => (\d+))] => [{">=" => 2000,
:note => "max_input_vars should be from 2000 to 5000 (or even more if needed)"},
{">=" => 2000}],
[:section, "Core", %r(^auto_prepend_file => (.+) => (.+))] => [{"eq" => "no value",
:note => "auto_prepend_file may cause issues"},
{"eq" => "no value"}],
[:section, "Core", %r(^auto_append_file => (.+) => (.+))] => [{"eq" => "no value",
:note => "auto_append_file may cause issues"},
{"eq" => "no value"}],
[:section, "Core", %r(^disable_functions => (.+) => (.+))] => [{"eq" => "no value",
:note => "disable_functions may cause issues"},
{"eq" => "no value"}],
[:section, "Core", %r(^open_basedir => (.+) => (.+))] => [{"eq" => "no value",
:note => "open_basedir is not supported."},
{"eq" => "no value"}],
[:section, "Core", %r(^safe_mode => (.+) => (.+))] => [{"eq" => "Off",
:note => "safe_mode is not supported."},
{"eq" => "Off"}],
[:section, "memcache", %r(^Version => (\S+))] => [{"eq" => "3.0.5",
:save => :memcache_version}],
[:section, "mysql", %r(^Client API version => (.+))] => [{:save => :mysql_client_api_version}],
}
end
class Php < Collection
def initialize(*args)
super
@children = [PhpV, PhpM, PhpI]
end
end
class Ps < ShellCommand
self.cmd = 'ps aux'
self.matches = {
%r(^root.*\s([/a-z]*apache2?\b)) => [{:alsocheck => Apache,
:save => :apache}],
%r(^root.*\s([/a-z]*httpd\b)) => [{:alsocheck => Apache,
:save => :apache}],
%r(nginx: master process .* -c (\S*)) => [{:alsocheck => NginxConf}],
%r(nginx: master process (\S*)) => [{:alsocheck => NginxExe,
:save => :nginx}],
%r((memcached).*-m (\d+)) => [{:save => :memcache},
{">=" => 1024}],
%r(redis-server\s+([\d\.]+:\d+)) => [{:alsocheck => Redis,
:save => :redis}],
%r(redis-server\s+(/[a-z\./]+)) => [{:alsocheck => RedisConf,
:save => :redis}],
%r(mysqld.*--log-error=([/\.\w-]+)) => [{:alsocheck => MysqlLog}],
%r(php-fpm: master process \((\S+)\)) => [{:alsocheck => PhpFpmConf}],
}