CollStorageWithCMD.java
5.08 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
package com.sitech.ismp.coll;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;
import org.apache.log4j.Logger;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.Formater;
public class CollStorageWithCMD {
/**
* case 1:
this.os_type = "AIX";
this.kbp_class = "10-14-12";
case 2:
this.os_type ="HP-UX";
this.kbp_class = "10-14-11";
case 3:
this.os_type = "Linux";
this.kbp_class = "10-14-13";
*/
private Logger logger = Logger.getLogger("COLL");
public String kbp_class="";
public String os_type ="";
public String host_name="";
public Vector collDiskStatus(HashMap params){
CollBase collResult = new CollBase();
//初始化参数
initkbpclass();
String net_host_name = Formater.neatenunitid(host_name);
if(this.os_type.equals("AIX")){
String cmd = "/usr/sbin/lsdev -Cc disk";
Vector result = this.execute(cmd);
if(result!=null&&result.size()>0){
for(int i=0;i<result.size();i++){
String disk_info = (String)result.get(i);
String diskname = split(disk_info,0);
String diskstatus = split(disk_info,1);
String unit_id = this.kbp_class+"-10:"+net_host_name+"-"+diskname;
//CM-00-05-002-04磁盘标识
//PM-00-05-800-02磁盘状态
//PM-00-05-800-03采集时间
collResult.addKPI(unit_id,
"CM-00-05-002-04", diskname);
collResult.addKPI(unit_id,
"PM-00-05-800-02", diskstatus);
collResult.addKPI(unit_id,
"PM-00-05-800-03", Formater.getNowTime());
}
}
}else if(this.os_type.equals("HP-UX")){
String cmd = "/sbin/ioscan -fknC disk";
Vector result = this.execute(cmd);
if(result!=null&&result.size()>0){
for(int i=0;i<result.size();i++){
String disk_info = (String)result.get(i);
if(disk_info!=null&&disk_info.indexOf("disk")==0){
String diskname = split(disk_info,1);
String diskstatus = split(disk_info,4);
String unit_id = this.kbp_class+"-10:"+net_host_name+"-disk"+diskname;
//CM-00-05-002-04磁盘标识
//PM-00-05-800-02磁盘状态
//PM-00-05-800-03采集时间
collResult.addKPI(unit_id,
"CM-00-05-002-04", "disk"+diskname);
collResult.addKPI(unit_id,
"PM-00-05-800-02", diskstatus);
collResult.addKPI(unit_id,
"PM-00-05-800-03", Formater.getNowTime());
}
}
}
}else if(this.os_type.equals("Linux")){
}
return collResult.getKPISet();
}
private void initkbpclass(){
this.getHostOS_TYPE();
this.getHostName();
}
private void getHostOS_TYPE(){
try{
String cmd = "uname -a";
Vector result = this.execute(cmd);
if(result!=null&&result.size()>0){
String s=(String) result.get(0);
//s = s.toLowerCase();
if(s.indexOf("AIX")>=0){
this.os_type = "AIX";
this.kbp_class = "10-14-12";
}else if(s.indexOf("HP-UX")>=0){
this.os_type ="HP-UX";
this.kbp_class = "10-14-11";
}else if(s.indexOf("Linux")>=0){
this.os_type = "Linux";
this.kbp_class = "10-14-13";
}
}
}catch(Exception e){
e.printStackTrace();
logger.error(e);
}
}
private void getHostName(){
try{
String cmd = "hostname";
Vector result = this.execute(cmd);
if(result!=null&&result.size()>0){
String s=(String) result.get(0);
this.host_name = s.trim();
}
}catch(Exception e){
e.printStackTrace();
logger.error(e);
}
}
// 执行命令开始
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 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 static void main(String[] arg){
HashMap params = new HashMap();
CollStorageWithCMD coll = new CollStorageWithCMD();
coll.collDiskStatus(params);
Vector lst = null;
try {
lst = coll.collDiskStatus(params);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("lst.size() is " + lst.size());
for (int i = 0; i < lst.size(); i++) {
TblATO_KPIDETAIL kpidetail = (TblATO_KPIDETAIL) lst.get(i);
System.out.println(kpidetail.UNIT_ID + "|" + kpidetail.KPI_ID + "|"
+ kpidetail.CLL_TIME + "|" + kpidetail.KPI_VALUE);
}
}
}