CollSqlServer2000BySql.java
6.8 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
package com.sitech.ismp.coll.database;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Vector;
import org.apache.log4j.Logger;
import com.sitech.ismp.coll.CollBase;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.Formater;
import com.sitech.util.LogUtil;
public class CollSqlServer2000BySql {
private Logger logger = Logger.getLogger(CollSqlServer2000BySql.class);
private String kbpClass = "10-11-34";
private String ip;
private String username;
private String password;
private String port;
private String dbserver;
private boolean isConnect = false;
private Connection conn = null;
public CollSqlServer2000BySql(HashMap<String, String> params) {
this.ip = params.get("IP_ADDR");
this.username = params.get("USER_NAME");
this.password = params.get("PASSWORD");
this.port = params.get("PORT");
this.dbserver = params.get("DBSERVER");
}
/**
* jdbc:sqlserver://172.21.3.100:1433
* @return
*/
public boolean init() {
String url = "jdbc:sqlserver://" + this.ip + ":" + this.port;
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
logger.info("Connect SQLServer 2000 Success: " + url);
isConnect = true;
} catch (Exception e) {
LogUtil.error("Exception while get SQLServer 2000 connection, URL=" + url, e);
isConnect = false;
}
return isConnect;
}
public void destroy() {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
logger.error("Exception while close SQLServer 2008 connection.", e);
}
}
conn = null;
}
private void clean(Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Vector<TblATO_KPIDETAIL> getConfig() {
logger.info("Begin getConfig...");
CollBase result = new CollBase();
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
// 数据库名 @数据库服务名 CM-00-03-001-08
result.addKPI(this.kbpClass + "-10:" + this.dbserver + "-SYS", "CM-00-03-001-08", dbserver);
String sqlTotal = "SELECT @@version Version";
rs = stmt.executeQuery(sqlTotal);
while (rs.next()) {
String version = rs.getString("Version");
String _version = version.substring(0, version.indexOf("\n"));
String unitIdTotal = this.kbpClass + "-10:" + this.dbserver + "-SYS";
// 数据库版本 CM-00-03-001-02
result.addKPI(unitIdTotal, "CM-00-03-001-02", _version.trim());
}
String sqlOther = "select dbid, name Logical_Name, crdate, filename, version from sysdatabases";
rs = stmt.executeQuery(sqlOther);
while (rs.next()) {
String logicName = rs.getString("Logical_Name");
String createDate = rs.getString("crdate");
String physicalName = rs.getString("filename");
String unitIdOther = this.kbpClass + "-11:" + this.dbserver + "-" + logicName;
// 数据库逻辑名@数据设备名
result.addKPI(unitIdOther, "CM-00-03-003-01", logicName);
// 数据库创建时间
result.addKPI(unitIdOther, "CM-00-03-001-07", createDate);
// 数据库物理文件路径
result.addKPI(unitIdOther, "CM-00-03-005-03", physicalName);
}
} catch (Exception e) {
logger.error("Exception while getVersion()", e);
} finally {
clean(stmt, rs);
}
logger.info("End getConfig...");
return result.KPISet;
}
public Vector<TblATO_KPIDETAIL> getSession() {
logger.info("Begin getSession...");
CollBase result = new CollBase();
CallableStatement stmtp = null;
ResultSet rs = null;
int n = 0;
try {
stmtp = conn.prepareCall("sp_who");
rs = stmtp.executeQuery();
while (rs.next()) {
n++;
}
String unitId = this.kbpClass + "-10:" + this.dbserver + "-SESSION";
// 会话数量 PM-00-03-009-03
result.addKPI(unitId, "PM-00-03-009-03", String.valueOf(n));
} catch (Exception e) {
logger.error("Exception while getSession()", e);
} finally {
clean(stmtp, rs);
}
logger.info("End getSession...");
return result.KPISet;
}
public Vector<TblATO_KPIDETAIL> getLock() {
logger.info("Begin getLock...");
CollBase result = new CollBase();
CallableStatement stmtp = null;
ResultSet rs = null;
int n = 0;
try {
stmtp = conn.prepareCall("sp_lock");
rs = stmtp.executeQuery();
while (rs.next()) {
n++;
}
String unitId = this.kbpClass + "-10:" + this.dbserver + "-LOCK";
// 锁数量 PM-00-03-008-01
result.addKPI(unitId, "PM-00-03-008-01", String.valueOf(n));
} catch (Exception e) {
logger.error("Exception while getLock()", e);
} finally {
clean(stmtp, rs);
}
logger.info("End getLock...");
return result.KPISet;
}
public Vector<TblATO_KPIDETAIL> getHelpdb() {
logger.info("Begin getHelpdb...");
CollBase result = new CollBase();
Statement stmt = null;
CallableStatement stmtp = null;
ResultSet rs = null;
ResultSet spaceusedrs = null;
try {
stmtp = conn.prepareCall("sp_helpdb");
rs = stmtp.executeQuery();
while (rs.next()) {
String name = rs.getString("name");
String dbsize = rs.getString("db_size");
dbsize = dbsize.substring(0, dbsize.length() - 3);
String owner = rs.getString("owner");
String dbid = rs.getString("dbid");
String created = rs.getString("created");
String status = rs.getString("status");
String unitId = this.kbpClass + "-11:" + this.dbserver + "-" + name;
try {
String sqlUse = "use " + name;
stmt = conn.createStatement();
stmt.execute(sqlUse);
String sql = "select size / 128 as used, name, filename, case maxsize / 128 when 0 then 'no limit' else cast(maxsize / 128 as varchar(10)) end as 'total(M)' from sysfiles where fileid = 1";
spaceusedrs = stmt.executeQuery(sql);
while (spaceusedrs.next()) {
String usedspace = spaceusedrs.getString("used");
Float unallocatedspace = Float.parseFloat(dbsize.trim()) - Float.parseFloat(usedspace.trim());
// 数据库空间剩余大小
result.addKPI(unitId, "PM-00-03-904-01", unallocatedspace + "");
String spaceused = Formater.dev2IntIntoFloat(usedspace.trim(), dbsize.trim(), 2);
// 数据库空间使用率
result.addKPI(unitId, "PM-00-03-004-01", spaceused);
}
} catch (Exception ex) {
logger.error("Exception while getVersion()", ex);
} finally {
clean(stmt, spaceusedrs);
}
// 数据库空间大小
result.addKPI(unitId, "CM-00-03-003-02", dbsize.trim());
}
} catch (Exception e) {
logger.error("Exception while getVersion()", e);
} finally {
clean(stmtp, rs);
}
logger.info("End getHelpdb...");
return result.KPISet;
}
}