CheckLinuxWithCMD.java
44.3 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
package com.sitech.ismp.check;
import java.util.Date;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;
import org.apache.log4j.Logger;
import com.sitech.ismp.app.coll.RPCTarget;
import com.sitech.ismp.coll.CollBase;
import com.sitech.util.Formater;
public class CheckLinuxWithCMD {
private Logger logger = Logger.getLogger("COLL");
String PRE_UNITID = "10-20-24"; // hp主机的标识
RPCTarget rpctarget = null;
String neat_host_name = "";
/*
* 智能巡检日志模块 包含:错误日志中是否有硬件错误
*/
public Vector getLog(HashMap params) {
try {
logger.info("Begin getLog");
init();
CollBase collResult = new CollBase();
String log_PRE_UNITID = PRE_UNITID + "-01";
String curdate = new Date().toString().substring(0, 10);
// 硬件错误
if (params.get("PM-20-21-001-01") != null
&& params.get("PM-20-21-001-01").equals("1")) {
String errorhlog = rpctarget
.getKPIValue("cat /var/adm/syslog/syslog.log|grep '"
+ curdate.substring(4, 10) + "'|grep EMS");
if (errorhlog.equals(""))
errorhlog = "NONE";
collResult.addKPI(log_PRE_UNITID + ":" + neat_host_name
+ "-log", "PM-20-21-001-01", errorhlog);
}
// 检查系统启动日志
if (params.get("PM-20-21-001-02") != null
&& params.get("PM-20-21-001-02").equals("1")) {
String uptime = rpctarget
.getKPIValue("ls -l /var/adm/rc.log|awk '{print $6\" \"$7\" \"$8}'");
collResult.addKPI(log_PRE_UNITID + ":" + neat_host_name
+ "-log", "PM-20-21-001-02", uptime);
}
// 硬件状态
/*
* if (params.get("PM-20-21-001-04") != null &&
* params.get("PM-20-21-001-04").equals("1")) { String errorlog =
* rpctarget .getKPIValue("cat /var/adm/syslog/syslog.log|grep -i
* error|grep '" + curdate.substring(4, 10) + "' |wc -l");
* collResult.addKPI(log_PRE_UNITID + ":" + neat_host_name + "-log",
* "PM-20-21-001-04", errorlog); }
*/
// 检查网络硬件日志
if (params.get("PM-20-21-001-03") != null
&& params.get("PM-20-21-001-03").equals("1")) {
String hasnew = rpctarget
.getKPIValue("netfmt -f /var/adm/nettl.LOG000|grep '"
+ curdate.substring(4, 10) + "'|wc -l");
String errornetlog = "";
if (hasnew.equals("0"))
errornetlog = "NONE";
else
errornetlog = rpctarget
.getKPIValue("netfmt -f /var/adm/nettl.LOG000|grep '"
+ curdate.substring(4, 10) + "'|tail -2");
collResult.addKPI(log_PRE_UNITID + ":" + neat_host_name
+ "-log", "PM-20-21-001-03", errornetlog);
}
//event.log日志检查
if (params.get("PM-20-21-001-68") != null
&& params.get("PM-20-21-001-68").equals("1")) {
String eventLog = rpctarget
.getKPIValue1(". ../bin/shell/eventLog.sh");
if(!eventLog.equals("NONE"))
eventLog = subString(eventLog);
collResult.addKPI(log_PRE_UNITID + ":" + neat_host_name
+ "-log", "PM-20-21-001-68",
eventLog);
}
return collResult.getKPISet();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* 智能巡检文件系统模块 包含:文件系统使用率是否接近阀值(90%)、是否有应该mount,但未mount的文件系统、nfs 文件系统是否能正常访问
*/
public Vector getFilesys(HashMap params) {
try {
logger.info("Begin getFilesys");
init();
String filesys_PRE_UNITID = PRE_UNITID + "-02:" + neat_host_name
+ "-filesys";
CollBase collResult = new CollBase();
// 使用率超过阀值的文件系统
if (params.get("PM-20-21-001-05") != null
&& params.get("PM-20-21-001-05").equals("1")) {
Vector filesys = rpctarget.getKPISet("bdf | sed -n '2,$p'");
String fsnames = "";
int filesys_used = 80;
if (params.get("FILESYS_USED") != null)
filesys_used = Integer.parseInt((String) params
.get("FILESYS_USED"));
for (int i = 0; i < filesys.size(); i++) {
String fsinfo = (String) filesys.elementAt(i);
String percent = split(fsinfo, 4);
String fsname = split(fsinfo, 5);
if(percent!=null&&fsname!=null&&!percent.trim().equals("")&&!fsname.trim().equals("")){
int percent_i = Integer.parseInt(subString(percent));
if (percent_i > filesys_used)
fsnames += fsname + ",";
}
}
fsnames = subString(fsnames);
if (fsnames.equals(""))
fsnames = "NONE";
collResult.addKPI(filesys_PRE_UNITID, "PM-20-21-001-05",
fsnames);
}
// closed状态的lv
if (params.get("PM-20-21-001-06") != null
&& params.get("PM-20-21-001-06").equals("1")) {
Vector vglist = rpctarget
.getKPISet("vgdisplay|grep -i 'VG Name'|awk '{print $3}'");
String closelvs = "";
for (int i = 0; i < vglist.size(); i++) {
String vgname = (String) vglist.elementAt(i);
Vector lvlist = rpctarget.getKPISet("vgdisplay -v "
+ vgname.trim()
+ "|grep -Ei 'lv name|lv status'|awk '{print $3}'");
for (int j = 0; j < lvlist.size() - 1; j = j + 2) {
String lvname = (String) lvlist.elementAt(j);
String lvstatus = (String) lvlist.elementAt(j + 1);
if (lvstatus.indexOf("available") == -1)
closelvs += lvname + ",";
}
}
closelvs = subString(closelvs);
if (closelvs.equals(""))
closelvs = "NONE";
collResult.addKPI(filesys_PRE_UNITID, "PM-20-21-001-06",
closelvs);
}
// nfs文件系统是否能正常访问
if (params.get("PM-20-21-001-07") != null
&& params.get("PM-20-21-001-07").equals("1")) {
Vector nfsfile = rpctarget.getKPISet("showmount -e");
String nfs = "";
for (int i = 0; i < nfsfile.size(); i++) {
String info = (String) nfsfile.elementAt(i);
if (info.indexOf("no exported file systems") > -1)
nfs = "无nfs文件系统";
else {
nfs = "Y";
break;
}
}
if(nfsfile.size()==0){
nfs = "无nfs文件系统";
}
collResult.addKPI(filesys_PRE_UNITID, "PM-20-21-001-07", nfs);
}
return collResult.getKPISet();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* 智能巡检进程模块 包含:占用cpu高的top10进程,进程总数,是否有僵尸进程,是否有父进程为1的非root用户进程存在
*/
public Vector getProcess(HashMap params) {
try {
logger.info("Begin getProcess");
init();
CollBase collResult = new CollBase();
String process_PRE_UNITID = PRE_UNITID + "-03";
// 占用cpu高的top10进程
if (params.get("PM-20-21-001-08") != null
&& params.get("PM-20-21-001-08").equals("1")) {
Vector topprocessV = rpctarget.getKPISet("top -d 1 -n 10");
String topprocess = (String) topprocessV.elementAt(0);
for (int i = 1; i < 10; i++) {
topprocess = topprocess + "、"
+ (String) topprocessV.elementAt(i);
}
collResult.addKPI(process_PRE_UNITID + ":" + neat_host_name
+ "-process", "PM-20-21-001-08", topprocess);
/*String topprocess = rpctarget.getKPIValue("top -d 1 -n 10|awk '{print $3\" \"$4\" \"$7\" \"$12}'|sed -n '3,$p'").trim();
collResult.addKPI(process_PRE_UNITID + ":" + neat_host_name
+ "-process", "PM-20-21-001-08", topprocess);*/
}
// 进程总数
if (params.get("PM-20-21-001-09") != null
&& params.get("PM-20-21-001-09").equals("1")) {
String totalprocess = rpctarget.getKPIValue(
"ps -ef | grep root |wc -l").trim();
collResult.addKPI(process_PRE_UNITID + ":" + neat_host_name
+ "-process", "PM-20-21-001-09", totalprocess);
}
// 是否有僵尸进程
if (params.get("PM-20-21-001-10") != null
&& params.get("PM-20-21-001-10").equals("1")) {
String zombieprocess = rpctarget.getKPIValue(
"ps -ef | grep defunct | grep -v grep | wc -l").trim();
collResult.addKPI(process_PRE_UNITID + ":" + neat_host_name
+ "-process", "PM-20-21-001-10", zombieprocess);
}
// 是否有父进程为1的非root用户进程存在
if (params.get("PM-20-21-001-11") != null
&& params.get("PM-20-21-001-11").equals("1")) {
String fatherprocessis1 = rpctarget
.getKPIValue(
"ps -ef|grep -v root|grep -v grep |awk '{if ($3==1) print}'|wc -l")
.trim();
collResult.addKPI(process_PRE_UNITID + ":" + neat_host_name
+ "-process", "PM-20-21-001-11", fatherprocessis1);
}
return collResult.getKPISet();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* 智能巡检安装模块 包含:操作系统版本,检查软件安装状态是否有不是configuard的,检查安装的软件是否有报错
*/
public Vector getInstall(HashMap params) {
try {
logger.info("Begin getInstall");
init();
CollBase collResult = new CollBase();
String install_PRE_UNITID = PRE_UNITID + "-05";
// 微码版本
if (params.get("PM-20-21-001-14") != null
&& params.get("PM-20-21-001-14").equals("1")) {
String fireware = rpctarget
.getKPIValue(
"/usr/contrib/bin/machinfo | grep \"Firmware revision\"|awk -F\"=\" '{print $2}'")
.trim();
if(fireware==null||fireware.equals("")){
fireware = "在本机没有machinfo文件";
}
collResult.addKPI(install_PRE_UNITID + ":" + neat_host_name
+ "-install", "PM-20-21-001-14", fireware);
}
// 操作系统版本
if (params.get("PM-20-21-001-15") != null
&& params.get("PM-20-21-001-15").equals("1")) {
String osverstion = rpctarget.getKPIValue(
"uname -a|awk '{print $3}'").trim();
collResult.addKPI(install_PRE_UNITID + ":" + neat_host_name
+ "-install", "PM-20-21-001-15", osverstion);
}
// 检查软件安装状态是否有不是configuard的
if (params.get("PM-20-21-001-16") != null
&& params.get("PM-20-21-001-16").equals("1")) {
String config = rpctarget
.getKPIValue(
"swlist -l fileset -a state|grep -v config|grep -v ^#|sed /^$/d|wc -l")
.trim();
String configornot = "N";
if (config.equals("0"))
configornot = "N";
else
configornot = "Y";
collResult.addKPI(install_PRE_UNITID + ":" + neat_host_name
+ "-install", "PM-20-21-001-16", configornot);
}
// 检查安装的软件是否有报错
if (params.get("PM-20-21-001-18") != null
&& params.get("PM-20-21-001-18").equals("1")) {
String swerrorlog = rpctarget
.getKPIValue("find /var/adm/sw/ -type f -name \"swinstall.log\"|xargs grep -i ERROR|wc -l");
if (swerrorlog.trim().equals("0"))
swerrorlog = "NONE";
else
swerrorlog ="错误数:"+swerrorlog;
collResult.addKPI(install_PRE_UNITID + ":" + neat_host_name
+ "-install", "PM-20-21-001-18", swerrorlog);
}
// /etc/rc.config.d/下netconf.bak的文件
if (params.get("PM-20-21-001-17") != null
&& params.get("PM-20-21-001-17").equals("1")) {
String netconfbak = rpctarget.getKPIValue(
"ls -1 /etc/rc.config.d|grep netconf.bak|wc -l").trim();
collResult.addKPI(install_PRE_UNITID + ":" + neat_host_name
+ "-install", "PM-20-21-001-17", netconfbak);
}
return collResult.getKPISet();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* 智能巡检Dumps模块 包含:dump 设备是否足够大
*/
public Vector getDumps(HashMap params) {
try {
logger.info("Begin getDumps");
init();
CollBase collResult = new CollBase();
String dumps_PRE_UNITID = PRE_UNITID + "-06";
// dump设备是否足够大
if (params.get("PM-20-21-001-19") != null
&& params.get("PM-20-21-001-19").equals("1")) {
String dumpssize = rpctarget
.getKPIValue(
"crashconf | grep -i Total | grep -i dump | awk -F ':' '{print $2}'")
.trim();
float dump_size = Float.parseFloat(dumpssize) / 1024;
collResult.addKPI(dumps_PRE_UNITID + ":" + neat_host_name
+ "-dumps", "PM-20-21-001-19", String
.valueOf(dump_size));
}
return collResult.getKPISet();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* 智能巡检交换空间模块 包含:交换空间总体使用率
*/
public Vector getInterspace(HashMap params) {
try {
logger.info("Begin getInterspace");
init();
CollBase collResult = new CollBase();
String interspace_PRE_UNITID = PRE_UNITID + "-07";
// 交换空间是否被使用
if (params.get("PM-20-21-001-20") != null
&& params.get("PM-20-21-001-20").equals("1")) {
Vector used = rpctarget
.getKPISet("swapinfo -amt|grep -E 'memory|total'|awk '{print $3}'");
String isUsed = "Y";
String memory_use = ((String) used.elementAt(0)).trim();
String total_use = ((String) used.elementAt(1)).trim();
if (memory_use.equals(total_use))
isUsed = "N";
collResult.addKPI(interspace_PRE_UNITID + ":" + neat_host_name
+ "-interspace", "PM-20-21-001-20", isUsed);
}
// 交换空间总体使用率
if (params.get("PM-20-21-001-21") != null
&& params.get("PM-20-21-001-21").equals("1")) {
Vector spaceusedV = rpctarget
.getKPISet("swapinfo -tam|grep -i total|awk '{print $5}'");
String spaceused = ((String) spaceusedV.elementAt(0)).trim();
collResult.addKPI(interspace_PRE_UNITID + ":" + neat_host_name
+ "-interspace", "PM-20-21-001-21", spaceused
.substring(0, spaceused.length() - 1));
}
return collResult.getKPISet();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* 智能巡检网络模块 包含:loopback/localhost 是否可以解析,
*/
public Vector getNetwork(HashMap params) {
try {
logger.info("Begin getNetwork");
init();
CollBase collResult = new CollBase();
String network_PRE_UNITID = PRE_UNITID + "-08";
// 网卡类型、工作模式、速率
Vector netcards = rpctarget
.getKPISet("lanscan|awk '{print $3\" \"$5\" \"$8\" \"$1}'|sed -n '3,$p'");
String card_mode = "";
String card_speed = "";
String card_types = "";
for (int i = 0; i < netcards.size(); i++) {
String card = (String) netcards.elementAt(i);
String cardidx = split(card, 0);
String cardname = split(card, 1);
String cardtype = split(card, 2);
String cardHardware = split(card,3);
card_types += cardname + ":" + cardtype + ":" + cardHardware + "#";
Vector card_info = rpctarget
.getKPISet("lanadmin -x "
+ cardidx
+ "|grep -E 'Current Config|Speed|Autonegotiation'|sed /^$/d");
for (int j = 0; j < card_info.size(); j++) {
String item = ((String) card_info.elementAt(j)).trim();
if (item.indexOf("Current Config") > -1)
card_speed += "lan"
+ cardidx
+ ":"
+ item.substring(item.indexOf("=") + 2, item
.length()) + "#";
if (item.indexOf("Autonegotiation") > -1)
card_mode += "lan" + cardidx + ":" + item + "#";
if (item.indexOf("Speed") > -1)
card_speed += "lan"
+ cardidx
+ ":"
+ item.substring(item.indexOf("=") + 2, item
.length() - 1) + "#";
}
}
card_mode = subString(card_mode);
card_speed = subString(card_speed);
if (params.get("PM-20-21-001-67") != null
&& params.get("PM-20-21-001-67").equals("1")) {
collResult.addKPI(network_PRE_UNITID + ":" + neat_host_name
+ "-network", "PM-20-21-001-67", card_types);
}
if (params.get("PM-20-21-001-22") != null
&& params.get("PM-20-21-001-22").equals("1")) {
collResult.addKPI(network_PRE_UNITID + ":" + neat_host_name
+ "-network", "PM-20-21-001-22", card_mode);
}
if (params.get("PM-20-21-001-23") != null
&& params.get("PM-20-21-001-23").equals("1")) {
collResult.addKPI(network_PRE_UNITID + ":" + neat_host_name
+ "-network", "PM-20-21-001-23", card_speed);
}
Vector gatewayV = rpctarget
.getKPISet("netstat -rn | grep default|awk '{print $2}'");
String gateway = ((String) gatewayV.elementAt(0)).trim();
// 当前路由
if (params.get("PM-20-21-001-24") != null
&& params.get("PM-20-21-001-24").equals("1")) {
String curroute = rpctarget
.getKPIValue("netstat -rn|sed -n '2,$p'|awk '{print $1}'");
collResult.addKPI(network_PRE_UNITID + ":" + neat_host_name
+ "-network", "PM-20-21-001-24", curroute);
}
// 路由设置是否正确(该指标影响别的newwork指标,暂时取消)
if (params.get("PM-20-21-001-25") != null
&& params.get("PM-20-21-001-25").equals("1")) {
String routeinfo = rpctarget.getKPIValue("traceroute -w 2 -q 1 -m 5 "
+ gateway.trim());
//String routeinfo = rpctarget.getKPIValue1(". ../bin/shell/setRouter.sh " + gateway.trim());
String routestate = "Y";
HashMap routefaile = new HashMap();
routefaile.put("!H", "主机不可到达");
routefaile.put("!N", "网络不可达");
routefaile.put("!P", "协议不可达");
routefaile.put("!S", "源路由失败");
routefaile.put("!F", "需要碎片");
int sidx = routeinfo.indexOf("!");
String status = routeinfo.substring(sidx, sidx + 2);
if (routeinfo.indexOf(status) > -1)
routestate = (String) routefaile.get(status);
collResult.addKPI(network_PRE_UNITID + ":" + neat_host_name
+ "-network", "PM-20-21-001-25", routestate);
}
// 缺省网关是否能ping通
if (params.get("PM-20-21-001-26") != null
&& params.get("PM-20-21-001-26").equals("1")) {
Vector pinggatewayV = rpctarget.getKPISet("ping " + gateway
+ " -n 2|grep loss|awk '{print $7}'");
String pinggateway = ((String) pinggatewayV.elementAt(0))
.trim();
String pinggatewayornot = "Y";
if (pinggateway.equals("100%"))
pinggatewayornot = "N";
else
pinggatewayornot = "Y";
collResult.addKPI(network_PRE_UNITID + ":" + neat_host_name
+ "-network", "PM-20-21-001-26", pinggatewayornot);
}
// 网络传输错误次数
if (params.get("PM-20-21-001-28") != null
&& params.get("PM-20-21-001-28").equals("1")) {
String errorsum = rpctarget
.getKPIValue("netstat -ni|grep -v Name|awk '{if($(NF-1)!=0) print $1\":\"$(NF-1)}'");
if (errorsum.trim().length()==0)
errorsum = "N";
collResult.addKPI(network_PRE_UNITID + ":" + neat_host_name
+ "-network", "PM-20-21-001-28",
errorsum);
}
// localhost是否可以解析
if (params.get("PM-20-21-001-29") != null
&& params.get("PM-20-21-001-29").equals("1")) {
Vector pinglocalhostV = rpctarget
.getKPISet("ping localhost -n 2|grep loss|awk '{print $7}'");
String pinglocalhost = ((String) pinglocalhostV.elementAt(0))
.trim();
String pinghostornot = "Y";
if (pinglocalhost.equals("100%"))
pinghostornot = "N";
collResult.addKPI(network_PRE_UNITID + ":" + neat_host_name
+ "-network", "PM-20-21-001-29", pinghostornot);
}
// 收集网络连接总数、各种状态的连接数
if (params.get("PM-20-21-001-30") != null
&& params.get("PM-20-21-001-30").equals("1")) {
String linksum = rpctarget
.getKPIValue(
"netstat -a|awk '$0 ~ /(LISTEN|ESTABLISHED|CLOSE_WAIT)/ {print $6}'|sort |uniq -c|awk '{print $2\":\"$1}'")
.trim();
collResult.addKPI(network_PRE_UNITID + ":" + neat_host_name
+ "-network", "PM-20-21-001-30", linksum);
}
// 检查是否有大量FIN_WAIT_2
if (params.get("PM-20-21-001-31") != null
&& params.get("PM-20-21-001-31").equals("1")) {
Vector finwait2V = rpctarget
.getKPISet("netstat -an | grep FIN_WAIT_2 | wc -l");
String finwait2 = ((String) finwait2V.elementAt(0)).trim();
collResult.addKPI(network_PRE_UNITID + ":" + neat_host_name
+ "-network", "PM-20-21-001-31", finwait2);
}
// hostname是否可以解析
if (params.get("PM-20-21-001-32") != null
&& params.get("PM-20-21-001-32").equals("1")) {
String hostname = rpctarget.getKPIValue("hostname").trim();
Vector pinghostnameV = rpctarget.getKPISet("ping " + hostname
+ " -n 2|grep loss|awk '{print $7}'");
String pinghostname = ((String) pinghostnameV.elementAt(0))
.trim();
String pinghostnameornot = "Y";
if (pinghostname.equals("100%"))
pinghostnameornot = "N";
collResult.addKPI(network_PRE_UNITID + ":" + neat_host_name
+ "-network", "PM-20-21-001-32", pinghostnameornot);
}
return collResult.getKPISet();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* 智能巡检vg00模块 包含:vg00剩余空间检查
*/
public Vector getVg00(HashMap params) {
try {
logger.info("Begin getVg00");
init();
CollBase collResult = new CollBase();
String vg00_PRE_UNITID = PRE_UNITID + "-09";
// vg00是否被正确的镜像
if (params.get("PM-20-21-001-34") != null
&& params.get("PM-20-21-001-34").equals("1")) {
String curpvcount = rpctarget.getKPIValue(
"vgdisplay vg00|grep -i 'Cur PV'|awk '{print $3}'")
.trim();
String actpvcount = rpctarget.getKPIValue(
"vgdisplay vg00|grep -i 'Act PV'|awk '{print $3}'")
.trim();
String isCorMirror = "Y";
if (!curpvcount.equals(actpvcount))
isCorMirror = "Cur PV:" + curpvcount + ";Act PV:"
+ actpvcount;
collResult.addKPI(vg00_PRE_UNITID + ":" + neat_host_name
+ "-vg00", "PM-20-21-001-34", isCorMirror);
}
// vg00 的启动盘是否正确镜像
if (params.get("PM-20-21-001-35") != null
&& params.get("PM-20-21-001-35").equals("1")) {
Vector startdisks = rpctarget
.getKPISet("lvlnboot -v|grep 'Boot Disk'|awk '{print $1}'");
String isMirror = "";
for (int i = 0; i < startdisks.size(); i++) {
String diskname = (String) startdisks.elementAt(i);
String diskstatus = rpctarget.getKPIValue(
"pvdisplay " + diskname
+ "|grep 'PV Status'|awk '{print $3}'")
.trim();
if (!diskstatus.equals("available"))
isMirror += diskname + ":" + diskstatus + ";";
}
if (isMirror.equals(""))
isMirror = "Y";
collResult.addKPI(vg00_PRE_UNITID + ":" + neat_host_name
+ "-vg00", "PM-20-21-001-35", isMirror);
}
// vg00剩余空间检查
if (params.get("PM-20-21-001-36") != null
&& params.get("PM-20-21-001-36").equals("1")) {
String freespace = rpctarget
.getKPIValue("vgdisplay vg00|awk '$0 ~ /Free/ {print $3\"MB\"}'");
collResult.addKPI(vg00_PRE_UNITID + ":" + neat_host_name
+ "-vg00", "PM-20-21-001-36", freespace);
}
// vg00的lv是否有不是sync的
if (params.get("PM-20-21-001-37") != null
&& params.get("PM-20-21-001-37").equals("1")) {
Vector lvlist = rpctarget
.getKPISet("vgdisplay -v /dev/vg00|grep -Ei 'LV Name|LV Status'|awk '{print $3}'");
String isSync = "";
for (int i = 0; i < lvlist.size() - 1; i = i + 2) {
String lvname = (String) lvlist.elementAt(i);
String lvstatus = (String) lvlist.elementAt(i + 1);
if (lvstatus.indexOf("sync") == -1)
isSync += lvname + ":" + lvstatus + ";";
}
if (isSync.equals(""))
isSync = "NONE";
collResult.addKPI(vg00_PRE_UNITID + ":" + neat_host_name
+ "-vg00", "PM-20-21-001-37", isSync);
}
return collResult.getKPISet();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* 智能巡检MCSG模块 包含:检查应用包日志、检查双机日志、MCSG调用启停脚本权限检查(读写权限)
*/
public Vector getMcsg(HashMap params) {
try {
logger.info("Begin getMCSG");
init();
CollBase collResult = new CollBase();
String mcsg_PRE_UNITID = PRE_UNITID + "-11:" + neat_host_name
+ "-mcsg";
// 是否发生ha切换
Vector clusters = rpctarget
.getKPISet("cmviewcl -l cluster|sed /^$/d|sed -n '2,$p'|awk '{print $1}'");
String is_switch = "未切换";
String ha_time = "";
String scri_right = "";
String package_name = "";
if (clusters.size()==0||clusters.indexOf("Permission denied to 127.0.0.1") > -1) {
is_switch = "未配置双击";
ha_time = "未配置双击";
scri_right = "未配置双击";
} else {
for (int i = 0; i < clusters.size(); i++) {
String cluster_name = (String) clusters.elementAt(i);
Vector node_list = rpctarget.getKPISet("cmviewcl -c "
+ cluster_name
+ " -l node|sed /^[[:space:]]*$/d|sed -n '2,$p'");
String loc_node = split((String) node_list.elementAt(0), 0);// 本身的节点
Vector package_list = rpctarget
.getKPISet("cmviewcl -n "
+ loc_node
+ " -l package|sed /^[[:space:]]*$/d|sed -n '2,$p'");
String package_info = (String) package_list.elementAt(0);
package_name = split(package_info, 0);
String cur_node = split(package_info, 4);// 目前所在的节点
if (!cur_node.equals(loc_node)) {
is_switch = "发生切换:" + loc_node + "->" + cur_node;
break;
}
ha_time = rpctarget.getKPIValue("ls -l /etc/cmcluster/|grep "
+ package_name
+ "|awk '{print $6\" \"$7\" \"$8}'");
scri_right = rpctarget.getKPIValue("ls -l /etc/cmcluster/"
+ package_name + "/st*|awk '{print $9\" \"$1}'");
}
}
// 是否发生ha切换
if (params.get("PM-20-21-001-40") != null
&& params.get("PM-20-21-001-40").equals("1")) {
collResult.addKPI(mcsg_PRE_UNITID, "PM-20-21-001-40",is_switch);
}
// ha切换时间
if (params.get("PM-20-21-001-41") != null
&& params.get("PM-20-21-001-41").equals("1")) {
collResult.addKPI(mcsg_PRE_UNITID, "PM-20-21-001-41", ha_time);
}
// ha切换脚本权限
if (params.get("PM-20-21-001-43") != null
&& params.get("PM-20-21-001-43").equals("1")) {
collResult.addKPI(mcsg_PRE_UNITID, "PM-20-21-001-43",
scri_right);
}
return collResult.getKPISet();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* 智能巡检Storage模块 包含:pv状态、链路状态、是否在HA里配置了磁盘切换脚本
*/
public Vector getStorage(HashMap params) {
try {
logger.info("Begin getStorage");
init();
CollBase collResult = new CollBase();
String storage_PRE_UNITID = PRE_UNITID + "-12:" + neat_host_name
+ "-storage";
// 链路状态不是CLAIMED的存储设备
if (params.get("PM-20-21-001-45") != null
&& params.get("PM-20-21-001-45").equals("1")) {
String linkstates = rpctarget
.getKPIValue("ioscan -fnCdisk|grep disk|grep -vE 'DVD|CLAIMED'|awk '{print $3\":\"$5}'");
if (linkstates.equals(""))
linkstates = "NONE";
collResult.addKPI(storage_PRE_UNITID, "PM-20-21-001-45",
linkstates);
}
// pv状态
if (params.get("PM-20-21-001-46") != null
&& params.get("PM-20-21-001-46").equals("1")) {
Vector vglist = rpctarget
.getKPISet("vgdisplay|grep -i 'VG Name'|awk '{print $3}'");
String closepvs = "";
for (int i = 0; i < vglist.size(); i++) {
String vgname = (String) vglist.elementAt(i);
Vector pvlist = rpctarget
.getKPISet("vgdisplay -v "
+ vgname.trim()
+ "|grep -Ei 'pv name|pv status'|grep -v 'Alternate Link'|awk '{print $3}'");
for (int j = 0; j < pvlist.size() - 1; j = j + 2) {
String pvname = (String) pvlist.elementAt(j);
String pvstatus = (String) pvlist.elementAt(j + 1);
if (pvstatus.indexOf("available") == -1)
closepvs += pvname + ",";
}
}
closepvs = subString(closepvs);
if (closepvs.length() == 0)
closepvs = "NONE";
collResult.addKPI(storage_PRE_UNITID, "PM-20-21-001-46",
closepvs);
}
return collResult.getKPISet();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* 智能巡检账号模块 包含:用户账号设置、用户组设置、用户口令设置、Root用户远程登录限制、系统用户登录限制
*/
public Vector getAccount(HashMap params) {
try {
logger.info("Begin getAccount");
init();
String account_PRE_UNITID = PRE_UNITID + "-13:" + neat_host_name
+ "-account";
CollBase collResult = new CollBase();
// 用户组设置
if (params.get("PM-20-21-001-49") != null
&& params.get("PM-20-21-001-49").equals("1")) {
String unusegroups = rpctarget
.getKPIValue1("cat /etc/group|grep -vE 'uucp|printq'|awk -F \":\" '{if($4==\"\") print $1}'");
if (unusegroups.length() == 0)
unusegroups = "N";
collResult.addKPI(account_PRE_UNITID, "PM-20-21-001-49",
unusegroups);
}
// Root用户远程登录限制
if (params.get("PM-20-21-001-50") != null
&& params.get("PM-20-21-001-50").equals("1")) {
String rootRlogin = rpctarget
.getKPIValue1("find /etc -type f -name \"\" |xargs cat |grep -i console|wc -l");
if (rootRlogin.trim().equals("0"))
rootRlogin = "N";
else
rootRlogin = "Y";
collResult.addKPI(account_PRE_UNITID, "PM-20-21-001-50",
rootRlogin);
}
// umask设置
if (params.get("PM-20-21-001-51") != null
&& params.get("PM-20-21-001-51").equals("1")) {
String umaskSet = rpctarget
.getKPIValue1("cat /.profile|grep umask|grep 027|grep -v grep|wc -l");
if (umaskSet.trim().equals("0"))
umaskSet = "N";
else
umaskSet = "Y";
collResult.addKPI(account_PRE_UNITID, "PM-20-21-001-51",
umaskSet);
}
return collResult.getKPISet();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* 智能巡检口令模块 包含:异常空口令账号、口令生存期安全、口令历史安全、口令锁定策略、口令到期安全、访问权限安全、FTP访问安全
*/
public Vector getPassword(HashMap params) {
try {
logger.info("Begin getFullcore");
init();
String password_PRE_UNITID = PRE_UNITID + "-14:" + neat_host_name
+ "-password";
CollBase collResult = new CollBase();
// 是否存在空口令账号
if (params.get("PM-20-21-001-52") != null
&& params.get("PM-20-21-001-52").equals("1")) {
String empPwd = rpctarget
.getKPIValue1(". ../bin/shell/emtPwd.sh");
collResult.addKPI(password_PRE_UNITID, "PM-20-21-001-52",
empPwd);
}
// 用户口令设置是否合规
if (params.get("PM-20-21-001-53") != null
&& params.get("PM-20-21-001-53").equals("1")) {
String secCfg = (String) params.get("secCfg");
String secPwd = rpctarget
.getKPIValue1(". ../bin/shell/secPwd.sh " + secCfg);
collResult.addKPI(password_PRE_UNITID, "PM-20-21-001-53",
secPwd);
}
// 口令锁定策略
if (params.get("PM-20-21-001-54") != null
&& params.get("PM-20-21-001-54").equals("1")) {
String lockVal = (String) params.get("umaxlntr");
String lockPwd = rpctarget
.getKPIValue1(". ../bin/shell/lockPwd.sh " + lockVal);
collResult.addKPI(password_PRE_UNITID, "PM-20-21-001-54",
lockPwd);
}
// 口令到期安全设置是否合规
if (params.get("PM-20-21-001-55") != null
&& params.get("PM-20-21-001-55").equals("1")) {
String expwarnSet = rpctarget
.getKPIValue1(". ../bin/shell/expwarnPwd.sh");
collResult.addKPI(password_PRE_UNITID, "PM-20-21-001-55",
expwarnSet);
}
return collResult.getKPISet();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* 智能巡检服务模块 包含:远程维护安全、禁用服务
*/
public Vector getService(HashMap params) {
try {
logger.info("Begin getFullcore");
init();
String service_PRE_UNITID = PRE_UNITID + "-15:" + neat_host_name
+ "-service";
CollBase collResult = new CollBase();
// 远程维护安全ssh、telnet检查
if (params.get("PM-20-21-001-56") != null
&& params.get("PM-20-21-001-56").equals("1")) {
String chkSer = "";
String isSsh = "";
String isTelnet = "";
String sshSer = rpctarget
.getKPIValue1("ps -elf|grep sshd|grep -v grep|wc -l");
String telnetSer = rpctarget
.getKPIValue1("ps -elf|grep telnetd|grep -v grep|wc -l");
if (sshSer.trim().equals("0"))
isSsh = "N";
else
isSsh = "Y";
if (telnetSer.trim().equals("0"))
isTelnet = "N";
else
isTelnet = "Y";
chkSer = "SSH:" + isSsh + ";Telnet:" + isTelnet;
collResult
.addKPI(service_PRE_UNITID, "PM-20-21-001-56", chkSer);
}
// 无用服务是否已禁用
if (params.get("PM-20-21-001-57") != null
&& params.get("PM-20-21-001-57").equals("1")) {
String nouseSer = rpctarget
.getKPIValue1("find /etc -type f -name \"inetd.conf\"|xargs cat|grep -E \"rexd|rusersd|rwalld|ttdbserver|cmsd|finger|ntalk\"|grep -v \\#|awk '{print $NF}'|awk -F. '{print $NF}'");
if (nouseSer.trim().length() == 0)
nouseSer = "Y";
collResult.addKPI(service_PRE_UNITID, "PM-20-21-001-57",
nouseSer);
}
return collResult.getKPISet();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public Vector getDevice(HashMap params) {
try {
logger.info("Begin getFullcore");
init();
String device_PRE_UNITID = PRE_UNITID + "-17:" + neat_host_name
+ "-device";
CollBase collResult = new CollBase();
// 超时设置
if (params.get("PM-20-21-001-61") != null
&& params.get("PM-20-21-001-61").equals("1")) {
String timeoutSet = rpctarget
.getKPIValue("cat /etc/profile|grep TMOUT|grep -v grep|wc -l");
if (timeoutSet.trim().equals("0"))
timeoutSet = "N";
else
timeoutSet = "Y";
collResult.addKPI(device_PRE_UNITID, "PM-20-21-001-61",
timeoutSet);
}
return collResult.getKPISet();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void init() {
rpctarget = new RPCTarget();
rpctarget.setRpcCmd("sh RPCTarget_suport.sh");
// 获取主机名称
String host_name = this.getHostName();
// 得到unit_id中使用的主机名称
neat_host_name = Formater.neatenunitid(host_name);
}
private String getHostName() {
Vector host = rpctarget.getKPISet("uname -a");
String host_name = (String) host.elementAt(0);
host_name = split(host_name, 1);
return host_name;
}
/**
* 如果超出了返回,返回 空串
*/
private String split(String str, int pos) {
StringTokenizer st = new StringTokenizer(str);
Vector values = new Vector();
while (st.hasMoreTokens()) {
values.add(st.nextToken());
}
if (pos >= 0 && pos < values.size()) {
return (String) values.elementAt(pos);
}
return "";
}
public String subString(String str) {
if (str != null && str.length() > 0)
str = str.substring(0, str.length() - 1);
return str;
}
public static void main(String[] args) throws InstantiationException,
IllegalAccessException {
String s = "Speed = 1000 Full-Duplex.";
System.out.print(s.substring(s.indexOf("=") + 1, s.length()));
}
}