CollLinuxWithCMD.java
41.4 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
package com.sitech.ismp.coll;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Vector;
import org.apache.log4j.Logger;
import com.sitech.ismp.app.coll.RPCTarget;
import com.sitech.util.Formater;
/**
* @author maozr
* 锟睫改版本锟斤拷 锟斤拷IBM锟斤拷HP锟斤拷锟缴硷拷指锟斤拷亩锟�锟斤拷位一锟斤拷,锟斤拷锟节达拷锟叫≈革拷辏–M-00-01-001-07锟斤拷锟缴硷拷锟斤拷锟斤拷值锟斤拷M
*/
public class CollLinuxWithCMD implements CollLinuxWithCMDMBean {
private Logger logger = Logger.getLogger("COLL");
String PRE_UNITID = "10-10-24"; // linux锟斤拷锟侥憋拷识
RPCTarget rpctarget = new RPCTarget();
private String getHostName() {
Vector host = execute("uname -a");
String host_name = (String) host.elementAt(0);
host_name = split(host_name, 1);
return host_name;
}
public 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 void init(HashMap params) {
rpctarget = new RPCTarget();
rpctarget.setRpcCmd("lexe_hp");
}
public Vector getConfig(HashMap params) {
// 锟斤拷锟斤拷杉锟斤拷锟斤拷锟斤拷锟斤拷值
CollBase collResult = new CollBase();
// 锟斤拷锟斤拷锟斤拷锟矫碉拷锟斤拷kpi值锟斤拷unit_id,锟斤拷锟斤拷PRE_UNITID + "-10"锟斤拷头锟斤拷锟斤拷锟絋otal锟斤拷锟�
String config_PRE_UNITID = PRE_UNITID + "-10"; // total值
// 锟斤拷取锟斤拷锟斤拷锟斤拷
Vector host = execute("uname -a");
String host_name = (String) host.elementAt(0);
String os_version = split((String) host.elementAt(0), 2);
host_name = collResult.split(host_name, 1);
// 锟矫碉拷unit_id锟斤拷使锟矫碉拷锟斤拷锟斤拷锟斤拷
String neat_host_name = Formater.neatenunitid(host_name);
// 锟斤拷硬杉锟绞憋拷锟街革拷锟�
String pattern = "yyyy-MM-dd-HH-mm-ss";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
collResult.addKPI(config_PRE_UNITID + ":" + neat_host_name + "-total",
"CM-00-01-001-22", dateFormat.format(new java.util.Date()));
// 锟斤拷锟斤拷ip,锟斤拷锟节告警锟斤拷锟斤拷
collResult.addKPI(config_PRE_UNITID + ":" + neat_host_name + "-total",
"CM-00-01-001-50", (String) params.get("IP_ADDR"));
// 锟斤拷锟斤拷锟斤拷
collResult.addKPI(config_PRE_UNITID + ":" + neat_host_name + "-total",
"CM-00-01-001-01", host_name);
// 锟斤拷锟斤拷锟斤拷锟�
collResult.addKPI(config_PRE_UNITID + ":" + neat_host_name + "-total",
"CM-00-01-001-03", "Linux");
// ----cpu锟斤拷息 锟斤拷 CM-00-01-001-04(锟斤拷锟斤拷)/05(锟酵猴拷)/06(锟斤拷频)-----
Vector cpunumV = execute("sh linux/cpucount.sh");
// 1 CM-00-01-001-04 cpu锟斤拷锟斤拷
// String cpu_size = String.valueOf(cpunumV.size());
String cpu_num = String.valueOf((String) cpunumV.elementAt(0));
collResult.addKPI(config_PRE_UNITID + ":" + neat_host_name + "-total",
"CM-00-01-001-04", Formater.formatDecimalKpivalue(cpu_num));
// 2 CM-00-01-001-05 cpu锟斤拷锟斤拷 CM-00-01-001-06 cpu锟斤拷频
Vector cputypeV = execute("sh linux/cputype.sh");
String cpu_types = (String) cputypeV.elementAt(0);
ArrayList cpu_temp = split(cpu_types," ");
String cpu_type =null,cpu_fre = null;
if(cpu_temp.size()==5){
cpu_type = split(cpu_types, 0) + " "
+ split(cpu_types, 1) + " "
+ split(cpu_types, 2) + " "
+ split(cpu_types, 3);
cpu_fre = split(cpu_types, 4);
}
if(cpu_temp.size()==4){
cpu_type = split(cpu_types, 0) + " "
+ split(cpu_types, 1) + " "
+ split(cpu_types, 2) + " ";
cpu_fre = split(cpu_types, 3);
}
else{
logger.info("file : cputype.sh size is wrong");
}
String cpu_hz = Float.parseFloat(cpu_fre.substring(0, cpu_fre.length() - 3)) * 1000 + "";
collResult.addKPI(config_PRE_UNITID + ":" + neat_host_name + "-total",
"CM-00-01-001-05", cpu_type);
collResult.addKPI(config_PRE_UNITID + ":" + neat_host_name + "-total",
"CM-00-01-001-06", cpu_hz);
Vector filerate_result = execute("sh linux/mem.sh");
String filerate_string = (String) filerate_result.elementAt(filerate_result.size() - 1);
String sub_msg[] = this.split2Array(filerate_string, " ");
String sum = sub_msg[0].trim();
float value = Float.parseFloat(sum) / 1024; // 2009-1-17, maozr锟叫革拷,锟斤拷位锟斤拷M
collResult.addKPI(config_PRE_UNITID + ":" + neat_host_name + "-total",
"CM-00-01-001-07", Formater.formatDecimalKpivalue(Float.toString(value)));
// 锟斤拷锟斤拷锟斤拷系统锟芥本 CM-00-01-001-08
collResult.addKPI(config_PRE_UNITID + ":" + neat_host_name + "-total",
"CM-00-01-001-08", os_version);
/*
* 锟斤拷锟斤拷锟缴硷拷,锟斤拷锟缴硷拷锟斤拷锟街革拷锟�:
* 系统锟斤拷锟斤拷涌锟絀P(CM-00-01-001-11)
* 系统锟斤拷锟斤拷涌锟斤拷锟斤拷锟斤拷址(CM-00-01-001-12)
* 锟斤拷锟斤拷锟阶刺�(FM-00-01-001-03)
* unit_id为:10-10-21-16:锟斤拷锟斤拷锟�-锟斤拷锟斤拷
*/
Vector lan = execute("/sbin/ifconfig -a");
int size = lan.size();
String net_unit = "10-10-24-16:" + neat_host_name;
String ip_addr = "local";
String phy_addr = "local";
String state = "down";
String network_name = "";
boolean phy_pass = true;
boolean ip_pass = true;
boolean state_pass = true;
boolean hard_pass = true;
int netcardnum = 0;
String service_ip = "";
// 锟斤拷锟斤拷址锟斤拷志位锟斤拷锟斤拷指锟斤拷锟饺�
for (int i = 0; i < size; i++) {
String network = (String) lan.elementAt(i);
// 锟斤拷锟斤拷锟�
int hard_flag = network.indexOf("Link encap");
if (hard_flag != -1 && hard_pass) {
network_name = network.substring(0, network.indexOf("Link encap")).trim();
if (network_name.indexOf("eth") < 0 || network_name.indexOf(":") > 0) {
// phy_pass = false;
// ip_pass = false;
// state_pass = false;
continue;
}
net_unit = net_unit + "-" + network_name;
hard_pass = false;
}
// 锟斤拷锟斤拷锟斤拷锟街凤拷锟饺�
int phy_flag = network.indexOf("HWaddr");
if (phy_flag != -1 && phy_pass) {
phy_addr = network.substring(network.indexOf("HWaddr") + 6).trim();
phy_pass = false;
}
// 锟斤拷取锟斤拷蠖ǖ锟絀P锟斤拷址
int ip_flag = network.indexOf("inet addr");
System.out.println("ip_flag1=" + ip_flag);
if (ip_flag != -1 && ip_pass) {
int index = network.indexOf("Bcast");
int inde1 = network.indexOf("Mask");
int tmp = (index == -1) ? inde1 : index;
ip_addr = network.substring(network.indexOf(":") + 1, tmp);
service_ip = service_ip + "," + ip_addr.trim();
ip_pass = false;
}
// 锟斤拷取锟斤拷状态
int state_flag = network.indexOf("RX bytes");
if (state_flag != -1 && state_pass) {
String send = network.substring(network.indexOf(":") + 1,
network.indexOf("(")).trim();
float sendNUM = Float.parseFloat(send);
if (sendNUM > 0) {
state = "up";
}
state_pass = false;
}
if (("".equals(network) || network.trim().length() == 0) && !hard_pass) {
// // 锟斤拷锟斤拷锟�
// collResult.addKPI(net_unit, "CM-00-01-100-01", network_name);
// // 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷址指锟斤拷
// collResult.addKPI(net_unit, "CM-00-01-100-03", phy_addr);
// // 锟斤拷锟斤拷锟絠p锟斤拷址指锟斤拷
// collResult.addKPI(net_unit, "CM-00-01-100-02", ip_addr);
// // 锟斤拷状态
// collResult.addKPI(net_unit, "FM-00-01-100-01", state);
// 锟斤拷锟斤拷锟�
collResult.addKPI(net_unit, "CM-00-01-001-19", network_name);
// 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷址指锟斤拷
collResult.addKPI(net_unit, "CM-00-01-001-12", phy_addr);
// 锟斤拷锟斤拷锟絠p锟斤拷址指锟斤拷
collResult.addKPI(net_unit, "CM-00-01-001-11", ip_addr);
// 锟斤拷状态
collResult.addKPI(net_unit, "FM-00-01-001-03", state);
phy_pass = true;
ip_pass = true;
state_pass = true;
hard_pass = true;
ip_addr = "local";
phy_addr = "local";
state = "down";
network_name = "";
net_unit = "10-10-24-16:" + neat_host_name;
netcardnum++;
}
}
// 锟斤拷锟斤拷锟今)碉拷址 CM-00-01-001-02锟斤拷 锟斤拷锟斤拷锟斤拷ip锟斤拷址锟桔硷拷
collResult.addKPI(config_PRE_UNITID + ":" + neat_host_name + "-total",
"CM-00-01-001-02", service_ip.substring(1, service_ip.length()));
// CM-00-01-001-10 系统锟斤拷锟斤拷涌锟斤拷锟�
collResult.addKPI(config_PRE_UNITID + ":" + neat_host_name + "-total",
"CM-00-01-001-10", String.valueOf(netcardnum));
// 锟斤拷锟斤拷锟斤拷锟斤拷锟叫� 锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷锟斤拷
String free_String = "free -m";
Vector free_value = execute(free_String);
int freeSize = free_value.size();
if (freeSize > 0) {
for (int i = 0; i < freeSize; i++) {
String _temp = (String) free_value.elementAt(i);
if (_temp.indexOf("Swap:") > -1) {
long swap_total = Long.parseLong(split(_temp, 1).trim());
long swap_free = Long.parseLong(split(_temp, 3).trim());
long rate = (swap_total - swap_free) * 100 / swap_total;
// 系统锟斤拷锟斤拷锟斤拷锟叫M-00-01-001-13
collResult.addKPI(config_PRE_UNITID + ":" + neat_host_name + "-total",
"CM-00-01-001-13", Long.toString(swap_total));
// 锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷锟斤拷PM-00-01-004-02
//10-10-24-20 为锟斤拷锟斤拷锟脚改的o拷锟矫伙拷要锟襟,斤拷锟斤拷锟斤拷锟斤拷锟絙锟斤拷4
collResult.addKPI("10-10-24-20"+ ":" + neat_host_name + "-total",
"PM-00-01-004-02", Formater.formatDecimalKpivalue(String.valueOf(rate)));
} else if (_temp.indexOf("buffers/cache:") > -1) {
long buffers_used = Long.parseLong(split(_temp, 2).trim());
long buffers_free = Long.parseLong(split(_temp, 3).trim());
long rate = buffers_used * 100 / (buffers_used + buffers_free);
// PM-00-01-002-08 锟侥硷拷系统锟斤拷莼锟斤拷锟斤拷锟斤拷锟斤拷锟�
collResult.addKPI(PRE_UNITID + "-12" + ":" + neat_host_name + "-memory",
"PM-00-01-002-08", Formater.formatDecimalKpivalue(String.valueOf(rate)));
}
}
}
// df -m | grep -v Use%
String disktotal = "df -m";
Vector disk_total = execute(disktotal);
int disksize = disk_total.size();
logger.info("df -m" + disksize);
int total = 0;
int usetotal = 0;
/*锟斤拷取锟斤拷锟斤拷硬锟教的达拷小锟斤拷锟斤拷锟节诧拷同锟斤拷Linux锟芥本锟斤拷锟斤拷锟斤拷锟筋‘df -m'锟斤拷
* 锟矫碉拷锟侥斤拷锟秸故撅拷锟斤拷锟酵拷锟斤拷锟斤拷薪锟斤拷锟斤拷锟斤拷卸希锟饺伙拷锟脚斤拷锟斤拷取值锟斤拷
*/
if (disksize > 0) {
for (int i = 0; i < disksize; i++) {
String _disktotal = (String) disk_total.elementAt(i);
if (_disktotal.indexOf("1M-") > -1) {
continue;
}
String str_disktotal_1 = split(_disktotal, 1).trim();
if (str_disktotal_1 == "") {
continue;
}
StringTokenizer stringTokenizer = new StringTokenizer(_disktotal);
String str_disktotal_2 = "";
if (stringTokenizer.countTokens() > 5) {
str_disktotal_2 = split(_disktotal, 2).trim();
str_disktotal_1 = split(_disktotal, 1).trim();
} else {
str_disktotal_2 = split(_disktotal, 1).trim();
str_disktotal_1 = split(_disktotal, 0).trim();
}
total += Integer.parseInt(str_disktotal_1);
usetotal += Integer.parseInt(str_disktotal_2);
}
float _usetotal = usetotal * 100 / total;
// CM-00-01-001-09 锟斤拷锟斤拷锟斤拷锟斤拷檀锟叫★拷锟斤拷艽锟叫★拷锟�
collResult.addKPI(config_PRE_UNITID + ":" + neat_host_name + "-total",
"CM-00-01-001-09", Formater.formatDecimalKpivalue(total + ""));
// 锟斤拷取锟侥硷拷系统使锟斤拷锟斤拷
collResult.addKPI(config_PRE_UNITID + ":" + neat_host_name + "-total",
"PM-00-01-004-01", Formater.formatDecimalKpivalue(_usetotal + ""));
}
// cluster锟斤拷息
String cluster_PRE_UNITID = PRE_UNITID + "-18"; // 锟斤拷群值
String cluster_cmd = "tail -7 /home/ibnms/masteragent/data/clusterstatus.txt";
Vector clusterV = execute(cluster_cmd);
boolean startread = false;
if (clusterV != null && clusterV.size() != 0) {
for (int i = 0; i < clusterV.size(); i++) {
String clusterInfo = (String) clusterV.elementAt(i);
if (clusterInfo.indexOf("AutoDisabled") > -1) {
startread = true;
}
if (startread && clusterInfo.trim() != null && clusterInfo.trim().length() > 0 && clusterInfo.indexOf("AutoDisabled") == -1) {
String target = (String) clusterV.elementAt(i);
String cluster_group = collResult.split(target, 1);
String cluster_name = collResult.split(target, 2);
String cluster_state = collResult.split(target, 5);
// 锟斤拷群锟斤拷锟斤拷锟�
collResult.addKPI(cluster_PRE_UNITID + ":" + neat_host_name
+ "-" + Formater.neatenunitid(cluster_group)
+ "_" + Formater.neatenunitid(cluster_name),
"CM-00-01-001-21", cluster_group);
// 锟斤拷群锟斤拷锟�
collResult.addKPI(cluster_PRE_UNITID + ":" + neat_host_name
+ "-" + Formater.neatenunitid(cluster_group)
+ "_" + Formater.neatenunitid(cluster_name),
"CM-00-01-001-18", cluster_name);
// 锟斤拷群状态
collResult.addKPI(cluster_PRE_UNITID + ":" + neat_host_name
+ "-" + Formater.neatenunitid(cluster_group)
+ "_" + Formater.neatenunitid(cluster_name),
"FM-00-01-001-04", cluster_state);
}
}
}
return collResult.getKPISet();
}
/**
* 锟缴硷拷cpu指锟斤拷
*/
public Vector getCpu(HashMap params) {
logger.info("begin getCpu");
init(params);
System.out.println("begin getCPU");
// 锟斤拷锟斤拷杉锟斤拷锟斤拷锟斤拷锟斤拷值
CollBase collResult = new CollBase();
// 锟斤拷锟斤拷锟斤拷锟矫碉拷锟斤拷kpi值锟斤拷unit_id,锟斤拷锟斤拷PRE_UNITID + "-11"锟斤拷头锟斤拷锟斤拷锟絚pu锟斤拷锟�
String cpu_PRE_UNITID = PRE_UNITID + "-11"; // total值
System.out.println("col host name");
// 锟斤拷取锟斤拷锟斤拷锟斤拷
String host_name = this.getHostName();
// 锟矫碉拷unit_id锟斤拷使锟矫碉拷锟斤拷锟斤拷锟斤拷
String neat_host_name = Formater.neatenunitid(host_name);
// 锟斤拷硬杉锟绞憋拷锟街革拷锟�
String pattern = "yyyy-MM-dd-HH-mm-ss";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
collResult.addKPI(cpu_PRE_UNITID + ":" + neat_host_name + "-cpu",
"CM-00-01-001-22", dateFormat.format(new java.util.Date()));
String value = ""; // 锟斤拷时锟斤拷
Vector cpuout = execute("vmstat 2 10");
String cpurun = (String) cpuout.elementAt(cpuout.size() - 1);
// CPU时锟戒:锟斤拷锟叫百分憋拷
collResult.addKPI(cpu_PRE_UNITID + ":" + neat_host_name + "-cpu",
"PM-00-01-001-01", Formater.formatDecimalKpivalue(collResult.split(cpurun, 14)));
// CPU时锟戒:系统锟劫分憋拷
collResult.addKPI(cpu_PRE_UNITID + ":" + neat_host_name + "-cpu",
"PM-00-01-001-02", Formater.formatDecimalKpivalue(collResult.split(cpurun, 13)));
// CPU时锟戒:锟矫伙拷锟劫分憋拷
collResult.addKPI(cpu_PRE_UNITID + ":" + neat_host_name + "-cpu",
"PM-00-01-001-03", Formater.formatDecimalKpivalue(collResult.split(cpurun, 12)));
// cpu锟斤拷锟斤拷锟斤拷, 100-idle
value = this.getCPU_Rate(3, 30);
collResult.addKPI(cpu_PRE_UNITID + ":" + neat_host_name + "-cpu",
"PM-00-01-001-05", value);
// CPU锟斤拷锟叫讹拷锟斤拷锟叫斤拷谈锟斤拷锟�
Vector vmstat_result = execute("vmstat 2 2");
String vmstat_string = (String) vmstat_result.elementAt(vmstat_result.size() - 1);
value = collResult.split(vmstat_string, 0);
collResult.addKPI(cpu_PRE_UNITID + ":" + neat_host_name + "-cpu",
"PM-00-01-001-06", Formater.formatDecimalKpivalue(value));
// CPU时锟戒:锟饺达拷俜直锟�
try {
Vector sar_cpuout = execute("sar -u 1 3");
// String sar_cpurun = (String) sar_cpuout.elementAt(cpuout.size() - 1);
// value = split(sar_cpurun, 5);
// collResult.addKPI(cpu_PRE_UNITID + ":" + neat_host_name + "-cpu",
// "PM-00-01-001-04", Formater.formatDecimalKpivalue(value));
for (int i = 0; i < sar_cpuout.size(); i++) {
String sar_cpurun = (String) sar_cpuout.elementAt(i);
if (sar_cpurun.indexOf("Average:") > -1) {
value = split(sar_cpurun, 5);
}
}
collResult.addKPI(cpu_PRE_UNITID + ":" + neat_host_name + "-cpu",
"PM-00-01-001-04", Formater.formatDecimalKpivalue(value));
} catch (Exception e) {
logger.info("cpu锟缴硷拷锟斤拷锟斤拷没锟斤拷sar锟斤拷锟斤拷!");
}
logger.info("end collCpu");
return collResult.getKPISet();
}
public Vector getDisk(HashMap params) {
logger.info("begin getActiveDisk");
init(params);
// 锟斤拷锟斤拷杉锟斤拷锟斤拷锟斤拷锟斤拷值
CollBase collResult = new CollBase();
// 锟斤拷锟斤拷锟斤拷锟矫碉拷锟斤拷kpi值锟斤拷unit_id,锟斤拷锟斤拷PRE_UNITID + "-13"锟斤拷头锟斤拷锟斤拷锟絛isk锟斤拷锟�
String disk_PRE_UNITID = PRE_UNITID + "-13";
// 锟斤拷取锟斤拷锟斤拷锟斤拷
String host_name = this.getHostName();
// 锟矫碉拷unit_id锟斤拷使锟矫碉拷锟斤拷锟斤拷锟斤拷
String neat_host_name = Formater.neatenunitid(host_name);
// 锟斤拷硬杉锟绞憋拷锟街革拷锟�
String pattern = "yyyy-MM-dd-HH-mm-ss";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
collResult.addKPI(disk_PRE_UNITID + ":" + neat_host_name + "",
"CM-00-01-001-22", dateFormat.format(new java.util.Date()));
String value = "";// 锟斤拷时锟斤拷
// 锟接筹拷锟斤拷Average锟斤拷锟斤拷锟叫匡拷始锟斤拷每一锟叫憋拷示一锟斤拷锟斤拷痰锟斤拷锟斤拷
try {
Vector diskV = execute("sar -d 5 5");
Set sarDiskSet = new HashSet(); // 锟斤拷锟斤拷锟斤拷锟斤拷sar锟斤拷锟斤拷锟斤拷取锟斤拷锟斤拷io锟借备锟斤拷锟�
for (int i = 0; i < diskV.size(); i++) {
String disk_value = (String) diskV.elementAt(i);
if (disk_value.indexOf("Average") < 0)
continue;
if (disk_value.indexOf("DEV") != -1)
continue;
String disk_name = split(disk_value, 1);
sarDiskSet.add(disk_name);
String neat_disk_name = Formater.neatenunitid(disk_name);
value = split(disk_value, 2);
collResult.addKPI(disk_PRE_UNITID + ":" + neat_host_name + "-"
+ neat_disk_name, "CM-00-01-001-20", neat_disk_name);
// 锟斤拷锟斤拷锟斤拷状态 FM-00-01-001-02
collResult.addKPI(disk_PRE_UNITID + ":" + neat_host_name + "-"
+ neat_disk_name, "FM-00-01-001-02", "CLAIMED");
// 锟斤拷锟斤拷锟斤拷锟斤拷IO锟斤拷锟斤拷锟斤拷锟斤拷
value = split(disk_value, 2);
collResult.addKPI(disk_PRE_UNITID + ":" + neat_host_name + "-"
+ neat_disk_name, "PM-00-01-003-01", Formater
.formatDecimalKpivalue(value));
// 每锟斤拷锟斤拷锟斤拷锟�
value = split(disk_value, 3);
collResult.addKPI(disk_PRE_UNITID + ":" + neat_host_name + "-"
+ neat_disk_name, "PM-00-01-003-04", Formater
.formatDecimalKpivalue(value));
// 每锟斤拷写锟斤拷锟斤拷
value = split(disk_value, 4);
collResult.addKPI(disk_PRE_UNITID + ":" + neat_host_name + "-"
+ neat_disk_name, "PM-00-01-003-05", Formater
.formatDecimalKpivalue(value));
// 锟斤拷锟斤拷忙锟侥百分憋拷
value = split(disk_value, 9);
collResult.addKPI(disk_PRE_UNITID + ":" + neat_host_name
+ "-" + neat_disk_name, "PM-00-01-003-03",
Formater.formatDecimalKpivalue((value.length() == 0) ? "0" : value));
// 锟饺达拷锟斤拷锟较低筹拷锟斤拷锟竭筹拷锟斤拷
value = split(disk_value, 6);
collResult.addKPI(disk_PRE_UNITID + ":" + neat_host_name
+ "-" + neat_disk_name, "PM-00-01-003-02",
Formater.formatDecimalKpivalue((value.length() == 0) ? "0" : value));
}
} catch (Exception e) {
logger.debug("disk 锟缴硷拷时没锟斤拷sar锟斤拷锟斤拷");
}
return collResult.getKPISet();
}
public Vector getFileSystem(HashMap params) {
logger.info("begin getFileSystem");
init(params);
// 锟斤拷锟斤拷杉锟斤拷锟斤拷锟斤拷锟斤拷值
CollBase collResult = new CollBase();
// 锟斤拷取锟斤拷锟斤拷锟斤拷
String host_name = this.getHostName();
// 锟矫碉拷unit_id锟斤拷使锟矫碉拷锟斤拷锟斤拷锟斤拷
String neat_host_name = Formater.neatenunitid(host_name);
// 锟缴硷拷锟斤拷锟侥硷拷系统占锟斤拷锟斤拷
Vector fileSystemV = execute("sh linux/filesys.sh");
int size = fileSystemV.size();
int max = 0;
try {
for (int i = 0; i < size; i++) {
String[] value = this.split2Array(((String) fileSystemV.get(i)), " ");
String file_sys_name = value[0];
String rate = value[4].substring(0, value[4].indexOf("%"));
String disk_name = value[5];
String max_size = value[1];
String available = value[3];
float avaliavle_tmp = Float.parseFloat(available.trim()) / 1024;
max = max + Integer.parseInt(max_size.trim());
// 锟侥硷拷系统 -14
collResult.addKPI(PRE_UNITID + "-14:" + neat_host_name + "-" + Formater.neatenunitid(disk_name) + "-" + file_sys_name,
"CM-00-01-001-16", disk_name);
collResult.addKPI(PRE_UNITID + "-14:" + neat_host_name + "-" + Formater.neatenunitid(disk_name) + "-" + file_sys_name,
"PM-00-01-004-03", Formater.formatDecimalKpivalue(rate));
collResult.addKPI(PRE_UNITID + "-14:" + neat_host_name + "-" + Formater.neatenunitid(disk_name) + "-" + file_sys_name,
"PM-00-01-004-04", Formater.formatDecimalKpivalue(Float.toString(avaliavle_tmp)));
// 锟竭硷拷锟斤拷 -17
collResult.addKPI(PRE_UNITID + "-17:" + neat_host_name + "-" + file_sys_name + "_" + Formater.neatenunitid(disk_name),
"CM-00-01-001-14", file_sys_name);
collResult.addKPI(PRE_UNITID + "-17:" + neat_host_name + "-" + file_sys_name + "_" + Formater.neatenunitid(disk_name),
"CM-00-01-001-15", Formater.formatDecimalKpivalue(Float.toString(avaliavle_tmp)));
}
} catch (Exception e) {
e.printStackTrace();
logger.error(e);
}
// 锟侥硷拷系统锟斤拷锟杰空硷拷
float value = Float.parseFloat(Integer.toString(max)) / 1000;
collResult.addKPI(PRE_UNITID + "-10" + ":" + neat_host_name + "-total",
"CM-00-01-001-17", Formater.formatDecimalKpivalue(Float.toString(value)));
return collResult.getKPISet();
}
/**
* 锟斤拷取锟斤拷锟斤拷锟矫伙拷锟矫伙拷锟斤拷锟斤拷锟�
*
* @param params
* @return
*/
public Vector getUserProNum(HashMap params) {
logger.info("begin getUserProNum ");
logger.info("begin getUserProName ");
init(params);
// 锟斤拷锟斤拷杉锟斤拷锟斤拷锟斤拷锟斤拷值
CollBase collResult = new CollBase();
// 锟矫碉拷锟斤拷锟斤拷锟矫伙拷锟斤拷锟叫�?锟斤拷锟矫伙拷锟斤拷锟斤拷侄锟斤拷俅危锟斤拷捅锟斤拷锟絧s锟斤拷4锟叫讹拷锟斤拷锟叫o拷锟斤拷锟叫讹拷锟劫斤拷锟�
Vector userV = new Vector();
Vector processV = execute("sh linux/process.sh");
for (int i = 0; i < processV.size(); i++) {
String processInfo = (String) processV.elementAt(i);
userV.add(collResult.split(processInfo, 0));
}
processV = null;
if (userV == null || userV.size() == 0) {
return new Vector();
}
// 锟斤拷锟斤拷锟斤拷锟矫伙拷锟侥斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟絤ap锟斤拷
// user - user process numer
Map user_pronum = new HashMap();
for (int i = 0; i < userV.size(); i++) {
String userName = ((String) userV.elementAt(i)).trim();
int pro_number = 1;
if (user_pronum.get(userName) == null) {
user_pronum.put(userName, new Integer(pro_number));
} else {
pro_number = Integer.parseInt(user_pronum.get(userName) + "") + 1;
user_pronum.put(userName, new Integer(pro_number));
}
}
// // 锟斤拷锟矫伙拷锟斤拷锟斤拷然锟斤拷锟斤拷锟斤拷锟斤拷
// java.util.Collections.sort(userV);
// String userName = "";
// for (int i = 0; i < userV.size(); i++) {
// userName = ((String) userV.elementAt(i)).trim();
// int pro_number = 1;
// String nextUer = "";
// for (int j = i + 1; j < userV.size(); j++) {
//
// // 锟斤拷锟斤拷锟斤拷一锟斤拷锟矫伙拷
// nextUer = ((String) (userV.elementAt(j))).trim();
// if (!userName.equals(nextUer)) {
// i = j - 1;
// break;
// }
// pro_number++;
// }
// user_pronum.put(userName, new Integer(pro_number));
// }
// 锟斤拷锟斤拷锟斤拷锟矫碉拷锟斤拷kpi值锟斤拷unit_id,锟斤拷锟斤拷PRE_UNITID + "-10"锟斤拷头锟斤拷锟斤拷锟絋otal锟斤拷锟�
String pro_PRE_UNITID = PRE_UNITID + "-15"; // total值
// 锟斤拷取锟斤拷锟斤拷锟斤拷
String host_name = this.getHostName();
// 锟矫碉拷unit_id锟斤拷使锟矫碉拷锟斤拷锟斤拷锟斤拷
String neat_host_name = Formater.neatenunitid(host_name);
Set users = user_pronum.keySet();
// System.out.println("users------------"+users);
Iterator iter = users.iterator();
while (iter.hasNext()) {
String username = (String) iter.next();
// System.out.println("username------------"+username);
collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-" + Formater.neatenunitid(username),
"PM-00-01-005-06", ((Integer) user_pronum.get(username)).toString());
collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-" + Formater.neatenunitid(username),
"CM-00-01-005-06", Formater.neatenunitid(username));
}
return collResult.getKPISet();
}
/**
* 锟斤拷锟斤拷锟斤拷锟节伙拷取指锟斤拷某锟斤拷锟教的革拷锟斤拷锟斤拷
*/
public Vector getSpecProcess(HashMap params) {
logger.info("begin getSpecProcess : " + params.get("PROCESS_NAME"));
init(params);
String pro_PRE_UNITID = PRE_UNITID + "-19";
// 锟斤拷锟斤拷杉锟斤拷锟斤拷锟斤拷锟斤拷值
CollBase collResult = new CollBase();
try {
String process_name = (String) params.get("PROCESS_NAME");
// 锟斤拷取锟斤拷锟斤拷锟斤拷
String host_name = getHostName();
// 锟矫碉拷unit_id锟斤拷使锟矫碉拷锟斤拷锟斤拷锟斤拷
String neat_host_name = Formater.neatenunitid(host_name);
// 时锟斤拷锟斤拷没锟斤拷 ":" , 锟斤拷么锟窖猴拷锟斤拷锟揭晃灰诧拷锟斤拷锟�
String cmd = "sh linux/specprocess.sh " + process_name;
// USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
// ibnms 12418 0.1 0.7 603192 31196 ? Sl 15:10 0:21 java -Dcqitsm=mx4j.server.MX4JMBeanServerBuilder com.sitech.jmx.manage.MasterAgent
Vector v1 = execute(cmd);
if (v1 == null || v1.equals("") || v1.equals("null")) {
// 锟斤拷锟斤拷锟节该斤拷锟�
collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-" + Formater.neatenunitid(process_name),
"PM-00-01-005-02", "DOWN");
return collResult.getKPISet();
} else {
for (int i = 0; i < v1.size(); i++) {
String pro_info = (String) v1.elementAt(i);
// 占锟斤拷CPU时锟斤拷 锟斤拷锟阶刺� 锟斤拷锟街革拷锟斤拷锟� 锟斤拷炭锟绞际憋拷锟� 锟斤拷痰墓锟侥�
String ppid = split(pro_info, 1);
String pcpu = split(pro_info, 9);
String pstatus = split(pro_info, 7);
if (pstatus.indexOf("S") > -1) {
pstatus = "Interruptible sleep";
} else if (pstatus.indexOf("R") > -1) {
pstatus = "Running";
} else if (pstatus.indexOf("T") > -1) {
pstatus = "Stopped";
} else if (pstatus.indexOf("W") > -1) {
pstatus = "paging";
} else if (pstatus.indexOf("X") > -1) {
pstatus = "dead";
} else if (pstatus.indexOf("Z") > -1) {
pstatus = "Defunct";
}
String pcmd = split(pro_info, 10) + " " + split(pro_info, 11);
String pstarttime = split(pro_info, 8);
String pmem = split(pro_info, 5);
// String puser = split(pro_info, 0);
// String pid = split(pro_info, 1);
// String pstime = split(pro_info, 4);
// pstime = pstime.indexOf(":") > -1 ? pstime : pstime + split(pro_info, 5); // 锟斤拷锟斤拷锟斤拷锟铰凤拷+锟斤拷锟绞憋拷锟斤拷锟斤拷
// int pos_command = pstime.indexOf(":") > 0 ? 7 : 8;
// String command = split(pro_info, pos_command);
// map1.put(process_name + ":" + pid, pstime + "," + puser);
// String key = process_name + ":" + ppid;
String key = process_name;
// 占锟斤拷CPU时锟斤拷
collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-" + key,
"PM-00-01-005-01", pcpu);
// 锟斤拷锟阶刺�
collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-" + key,
"PM-00-01-005-02", pstatus);
// 锟斤拷锟街革拷锟斤拷锟�
collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-" + key,
"PM-00-01-005-03", pcmd);
// 锟斤拷炭锟绞际憋拷锟�
collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-" + key,
"PM-00-01-005-04", pstarttime);
// 锟斤拷痰墓锟侥�
collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-" + key,
"PM-00-01-005-05", pmem);
// 锟斤拷锟斤拷锟�
collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-" + key,
"PM-00-01-005-07", process_name);
}
// collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-"
// + Formater.neatenunitid(process_name),
// "PM-00-01-005-06", v1);
// if (max_count == -1) {
// if (count >= min_count) {
// collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name
// + "-" + Formater.neatenunitid(process_name),
// "PM-00-01-005-06", "natural");
// } else {
// collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name
// + "-" + Formater.neatenunitid(process_name),
// "PM-00-01-005-06", "abnormity");
// }
// } else {
// if (count >= min_count && count < max_count) {
// collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name
// + "-" + Formater.neatenunitid(process_name),
// "PM-00-01-005-06", "natural");
// } else {
// collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name
// + "-" + Formater.neatenunitid(process_name),
// "PM-00-01-005-06", "abnormity");
// }
// }
}
} catch (Exception e) {
e.printStackTrace();
}
logger.info("end getSpecProcess : " + params.get("PROCESS_NAME"));
return collResult.getKPISet();
}
/**
* 锟缴硷拷锟节达拷指锟斤拷
*/
public Vector getMemory(HashMap params) {
logger.info("begin getMemory");
init(params);
// 锟斤拷锟斤拷杉锟斤拷锟斤拷锟斤拷锟斤拷值
CollBase collResult = new CollBase();
// 锟斤拷锟斤拷锟斤拷锟矫碉拷锟斤拷kpi值锟斤拷unit_id,锟斤拷锟斤拷PRE_UNITID + "-12"锟斤拷头
String memory_PRE_UNITID = PRE_UNITID + "-12";
// 锟斤拷取锟斤拷锟斤拷锟斤拷
String host_name = this.getHostName();
// 锟矫碉拷unit_id锟斤拷使锟矫碉拷锟斤拷锟斤拷锟斤拷
String neat_host_name = Formater.neatenunitid(host_name);
// 锟斤拷硬杉锟绞憋拷锟街革拷锟�
String pattern = "yyyy-MM-dd-HH-mm-ss";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name + "",
"CM-00-01-001-22", dateFormat.format(new java.util.Date()));
Vector vmstat_result = execute("vmstat 2 10");
String vmstat_string = (String) vmstat_result.elementAt(vmstat_result.size() - 1);
String value = "";
// PM-00-01-002-02 锟节存交锟斤拷锟斤拷锟斤拷锟斤拷
value = split(vmstat_string, 8); // -锟斤拷锟街革拷锟斤拷应锟斤拷锟斤拷HP vmstat锟叫的碉拷9锟叫碉拷值,锟�<苹锟斤拷锟揭恍╋拷锟斤拷锟�
collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name + "-memory",
"PM-00-01-002-02", Formater.formatDecimalKpivalue(value));
// PM-00-01-002-03 锟节存交锟斤拷页锟斤拷锟斤拷锟斤拷
value = split(vmstat_string, 6);
collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name + "-memory",
"PM-00-01-002-03", Formater.formatDecimalKpivalue(value));
// PM-00-01-002-04 锟节存交锟斤拷页锟斤拷锟斤拷锟斤拷
value = split(vmstat_string, 7);
collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name + "-memory",
"PM-00-01-002-04", Formater.formatDecimalKpivalue(value));
// PM-00-01-002-05 锟节达拷锟斤拷锟斤拷锟�
value = split(vmstat_string, 0);
collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name + "-memory",
"PM-00-01-002-05", Formater.formatDecimalKpivalue(value));
try {
// logger.info("filerate_string");
// Vector filerate_result = execute("sh linux/mem.sh");
// String filerate_string = (String) filerate_result
// .elementAt(filerate_result.size() - 1);
// String sub_msg[] = this.split2Array(filerate_string, " ");
// String sum = sub_msg[0].trim();
// String used = sub_msg[1].trim();
//
// float _count = Float.parseFloat(sum);
// float _free = Float.parseFloat(used);
// float _used = _count - _free;
// float _persent = (_used / _count) * 100;
// String persent = Formater.formatDecimalKpivalue(String.valueOf(_persent));
//
// // PM-00-01-002-08 锟侥硷拷系统锟斤拷莼锟斤拷锟斤拷锟斤拷锟斤拷锟�
// collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name
// + "-memory", "PM-00-01-002-08", persent);
logger.info("filerate_string_rate");
Vector filerate_result_rate = execute("sh linux/memrate.sh");
String filerate_string_rate = (String) filerate_result_rate
.elementAt(filerate_result_rate.size() - 1);
String all[] = this.split2Array(filerate_string_rate, ":");
String sum_ = all[0].trim();
String used_ = all[1].trim();
float rate_count = Float.parseFloat(sum_);
float rate_used = Float.parseFloat(used_);
float rate_persent = (rate_used / rate_count) * 100;
String rate_persent_value = Formater.formatDecimalKpivalue(String
.valueOf(rate_persent));
// PM-00-01-002-01 锟节达拷使锟斤拷锟斤拷
collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name
+ "-memory", "PM-00-01-002-01", rate_persent_value);
// 锟矫伙拷锟节达拷使锟斤拷锟斤拷 & 系统锟节达拷使锟斤拷锟斤拷 ps aux | awk '{memory +=$4}; END {print memory }'
// Vector sysusermemV = execute("sh linux/sysusermem.sh");
// String usermem = sysusermemV.elementAt(0).toString();
// String sysmem = "";
//// float usermemrate = Float.parseFloat(usermem);
// float sysmemrate = Float.parseFloat(rate_persent_value) - Float.parseFloat(usermem);
// // PM-00-01-002-07 锟矫伙拷锟节达拷使锟斤拷锟斤拷
// collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name
// + "-memory", "PM-00-01-002-07", Formater.formatDecimalKpivalue(usermem));
// // PM-00-01-002-06 系统锟节达拷使锟斤拷锟斤拷
// collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name
// + "-memory", "PM-00-01-002-06", Formater.formatDecimalKpivalue(String.valueOf(sysmemrate)));
// PM-00-01-002-07 锟矫伙拷锟节达拷使锟斤拷锟斤拷
collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name
+ "-memory", "PM-00-01-002-07", Formater.formatDecimalKpivalue(rate_persent * 0.68 + ""));
// PM-00-01-002-06 系统锟节达拷使锟斤拷锟斤拷
collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name
+ "-memory", "PM-00-01-002-06", Formater.formatDecimalKpivalue(rate_persent * 0.32 + ""));
} catch (Exception e) {
logger.error(e);
}
logger.info("end getMemory");
return collResult.getKPISet();
}
public ArrayList split(String str, String sp) {
int ip = 0;
ArrayList al = new ArrayList();
ip = str.indexOf(sp);
if (ip != -1) {
al.add(str.substring(0, ip));
str = str.substring(ip + sp.length());
if (str.length() != 0) {
al.addAll(split(str, sp));
}
} else {
al.add(str);
}
return al;
}
/**
* 锟缴硷拷cpu锟斤拷锟斤拷锟绞o拷锟斤拷锟缴硷拷times锟轿o拷每锟轿采硷拷锟斤拷锟絠nterval锟斤拷
*
* @param times
* @param interval
* @return
*/
private String getCPU_Rate(int times, int interval) {
CollBase collResult = new CollBase();
float cpu_value = 0;
for (int i = 0; i < times; i++) {
Vector cpuout = this.execute("vmstat 1 3");
String cpurun = (String) cpuout.elementAt(cpuout.size() - 1);
cpu_value = (100 - Integer.parseInt(collResult.split(cpurun, 14))) + cpu_value;
try {
Thread.sleep(interval * 1000L);
} catch (Exception e) {
e.printStackTrace();
}
}
cpu_value = cpu_value / times;
return Formater.formatDecimalKpivalue(Float.toString(cpu_value));
}
public String[] split2Array(String str, String sp) {
ArrayList al = split(str, sp);
Object[] os = al.toArray();
String[] ss = new String[os.length];
for (int i = 0; i < os.length; i++) {
ss[i] = (String) os[i];
}
return ss;
}
// 执锟斤拷锟斤拷锟筋开始
private Vector execute(String shellCommand) {
logger.info("cmd: " + shellCommand);
Vector vResult = null;
try {
Process process = Runtime.getRuntime().exec(shellCommand);
vResult = new Vector();
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = reader.readLine();
while (line != null) {
if (shellCommand.indexOf("cqitsm") > 0) {
logger.info("line: " + line);
}
vResult.addElement(line);
line = reader.readLine();
}
reader.close();
return vResult;
} catch (Exception e) {
logger.error("锟节憋拷锟斤拷锟较匡拷锟斤拷没锟斤拷 " + shellCommand + " 锟斤拷锟斤拷!");
} finally {
Runtime.getRuntime().freeMemory();
Runtime.getRuntime().gc();
}
return vResult;
}
public Vector getAllProcess(HashMap params){
logger.info("begin getAllDisk");
init(params);
String pro_PRE_UNITID = PRE_UNITID + "-15"; // total值
// 锟斤拷锟斤拷杉锟斤拷锟斤拷锟斤拷锟斤拷值
CollBase collResult = new CollBase();
String host_name = this.getHostName();
// 锟矫碉拷unit_id锟斤拷使锟矫碉拷锟斤拷锟斤拷锟斤拷
String neat_host_name = Formater.neatenunitid(host_name);
String unit_process = "sh linux/process.sh";
/* String unit_process = "ps -ef -o pcpu,state,sz,pid,ruser,stime,args | grep \""
+ process_name + "\" | grep -v grep | grep -v '%CPU'";
*/ logger.info("process_name_cmd="+unit_process);
Vector proV = rpctarget.getKPISet(unit_process);
for (int i = 0; i < proV.size(); i++) {
String pro_info = (String) proV.elementAt(i);
if(pro_info==null||pro_info.trim().equals("")){
continue;
}
// 占锟斤拷CPU时锟斤拷 锟斤拷锟阶刺� 锟斤拷锟街革拷锟斤拷锟� 锟斤拷炭锟绞际憋拷锟� 锟斤拷痰墓锟侥�
String ppid = split(pro_info, 1);
String pcpu = split(pro_info, 9);
String pstatus = split(pro_info, 7);
if (pstatus.indexOf("S") > -1) {
pstatus = "Interruptible sleep";
} else if (pstatus.indexOf("R") > -1) {
pstatus = "Running";
} else if (pstatus.indexOf("T") > -1) {
pstatus = "Stopped";
} else if (pstatus.indexOf("W") > -1) {
pstatus = "paging";
} else if (pstatus.indexOf("X") > -1) {
pstatus = "dead";
} else if (pstatus.indexOf("Z") > -1) {
pstatus = "Defunct";
}
String pcmd = split(pro_info, 10) + " " + split(pro_info, 11);
String pstarttime = split(pro_info, 8);
String pmem = split(pro_info, 5);
String puser = split(pro_info, 0);
String pid = split(pro_info, 1);
String pstime = split(pro_info, 4);
pstime = pstime.indexOf(":") > -1 ? pstime : pstime + split(pro_info, 5); // 锟斤拷锟斤拷锟斤拷锟铰凤拷+锟斤拷锟绞憋拷锟斤拷锟斤拷
int pos_command = pstime.indexOf(":") > 0 ? 7 : 8;
String command = split(pro_info, pos_command);
String key = ""+i;
// 占锟斤拷CPU时锟斤拷
collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-" + key,
"TM-00-01-005-01", pcpu);
// 锟斤拷锟阶刺�
collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-" + key,
"TM-00-01-005-02", pstatus);
// 锟斤拷锟街革拷锟斤拷锟�
collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-" + key,
"TM-00-01-005-03", pcmd);
// 锟斤拷炭锟绞际憋拷锟�
collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-" + key,
"TM-00-01-005-04", pstarttime);
// 锟斤拷痰墓锟侥�
collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-" + key,
"TM-00-01-005-05", pmem);
/*// 锟斤拷锟斤拷锟�
collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-" + key,
"TM-00-01-005-07", command);*/
collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-" + key,
"TM-00-01-005-08", pid);
collResult.addKPI(pro_PRE_UNITID + ":" + neat_host_name + "-" + key,
"TM-00-01-005-09", puser);
}
return collResult.getKPISet();
}
}