📄 parser.java
字号:
package com.ideas.parser;
import com.ideas.util.*;
import java.util.Vector;
import java.util.regex.*;
import java.sql.*;
import com.ideas.util.Tools;
import java.util.*;
public class Parser {
DBConnectionManager connMgr = null;
PreparedStatement pstmt = null;
private String ipaddress;
Connection con = null;
/*每一条记录最后都会有当前采集的时间戳,
以及采集点的ip地址,都要插入到数据库*/
Timestamp timestamp;
public Parser() {
connMgr = DBConnectionManager.getInstance();
con = connMgr.getConnection(Configuration.ConnectionPoolName);
if (this.con == null) {
System.out.println("不能获取数据库连接!!");
return;
}
timestamp = new Timestamp(new java.util.Date().getTime());
//System.out.println("timestamp: "+timestamp);
}
public void parse(String content) {
//过滤尾部多余字符
content = content.substring(0, content.lastIndexOf("}") + 1);
//去掉%
content = content.replaceAll("%", "");
String header = null;
String[] body = null;
setIpAddress(content);
try {
String[] items = getItems(content);
for (int i = 0; i < items.length; i++) {
header = getHeader(items[i]);
//System.out.println(header);
body = getBody(items[i]);
// for (int k = 0; k < body.length; k++) {
// System.out.println(body[k]);
//}
pushItemToDatabase(header, body);
}
if (pstmt != null) {
pstmt.close();
}
connMgr.freeConnection(Configuration.ConnectionPoolName, con);
}
catch (Exception e) {
//System.err.println("数据源格式不规范!!\n" + content);
System.err.println(e.getLocalizedMessage());
connMgr.freeConnection(Configuration.ConnectionPoolName, con);
}
}
/**
* 从一个字符串中查找一个ip地址
* @param content
* @return
*/
public void setIpAddress(String content) {
Pattern p = null;
Matcher m = null;
p = Pattern.compile("[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}");
m = p.matcher(content);
if (m.find()) {
this.ipaddress = m.group();
}
}
public String findValue(String body) {
String value = "";
Pattern p = null;
Matcher m = null;
//正则表达式
//至少一次“=”
//中间可以是任意多个任何字符(除了“,”)
//以或不以“,”结尾
p = Pattern.compile("={1}[^,]*,{0,1}");
m = p.matcher(body);
if (m.find()) {
value = m.group();
value = value.substring(1);
}
return value.trim();
}
public String getIpAddress() {
return this.ipaddress;
}
/**
* 如:从客户端的字符流中得到信息项
* cpu{user=0 ,sys=2 ,wio=0 ,idle=98}
* @param src
* @return
* @throws java.lang.Exception
*/
public String[] getItems(String src) throws Exception {
Pattern p = Pattern.compile("[}]{1}");
String[] result = p.split(src);
return result;
}
/**
*得到信息项的头信息
* 如:cpu{user=0 ,sys=2 ,wio=0 ,idle=98}中的cpu
* @param src
* @return
* @throws java.lang.Exception
*/
public String getHeader(String item) throws Exception {
String header = item.substring(0, item.indexOf("{")).trim();
return header;
}
/**
*得到信息项的体信息
* 如:cpu{user=0 ,sys=2 ,wio=0 ,idle=98}中的user=0 ,sys=2 ,wio=0 ,idle=98
* @param src
* @return
* @throws java.lang.Exception
*/
public String[] getBody(String item) throws Exception {
Pattern p = Pattern.compile("[,]{1}");
String[] result = p.split(item.substring(item.indexOf("{") + 1,
item.length()));
return result;
}
/**
* sybase 的不规范engine纪录
* @param header
* @param body
*/
public void pushSybaseItemsToDatabase(String header, String[] body,
int Colums) throws Exception {
if (Tools.isNULL(header) || Tools.isNULL(header)) {
return;
}
String servername = findValue(body[0]);
String sql = "";
try {
String fields = "";
for (int i = 1; i < Colums + 1; i++) {
fields += body[i].substring(0, body[i].indexOf("=")) + ",";
}
fields += "server,timestamp,ipaddress"; //比一般的纪录多了server字段
sql = "insert into " + header + "(" + fields + ") values ( ";
for (int i = 0; i < Colums + 3; i++) {
if (i == 0) {
sql += "?";
}
else {
sql += ",?";
}
}
sql += ")";
pstmt = con.prepareStatement(sql);
int rowlength = (body.length - 1) / Colums; //engine 的字段数是2,减去头部的server=
for (int row = 0; row < rowlength; row++) {
int i = 1;
for (; i < Colums + 1; i++) {
pstmt.setString(i, findValue(body[row * Colums + i])); //去掉body[0] ="server='rs6000'"
}
pstmt.setString(i, servername);
pstmt.setTimestamp(i + 1, timestamp);
pstmt.setString(i + 2, getIpAddress());
pstmt.executeUpdate();
pstmt.close();
}
}
catch (Exception e) {
System.err.println("数据库操作失败!在操纵 " + header + " 表时出现异常。");
e.printStackTrace();
throw e;
}
}
/**
* 多行规范纪录
* @param header
* @param body
* @param number ,信息项个数
*/
public void pushSpecItemToDatabase(String header, String[] body, int number) throws
Exception {
if (Tools.isNULL(header) || (body == null)) {
return;
}
String sql = "";
try {
String fields = "";
for (int i = 0; i < number; i++) {
if (i == 0) {
fields += body[i].substring(0, body[i].indexOf("="));
}
else {
fields += "," + body[i].substring(0, body[i].indexOf("="));
}
}
fields += ",timestamp,ipaddress";
sql = "insert into " + header + "(" + fields + ") values ( ";
for (int i = 0; i < number + 2; i++) {
if (i == 0) {
sql += "?";
}
else {
sql += ",?";
}
}
sql += ")";
pstmt = con.prepareStatement(sql);
/*每一条记录最后都会有当前采集的时间戳
(同一批纪录采用同一个时间戳)*/
java.util.Date utilDate = new java.util.Date();
int rowlength = body.length / number;
for (int row = 1; row < rowlength + 1; row++) {
int i = 1;
for (; i < number + 1; i++) {
pstmt.setString(i, findValue(body[ (row - 1) * number + i - 1]));
}
pstmt.setTimestamp(i, timestamp);
/** 以及采集点的ip地址,都要插入到数据库*/
pstmt.setString(i + 1, getIpAddress());
pstmt.executeUpdate();
}
pstmt.close();
}
catch (Exception e) {
System.err.println("数据库操作失败!在操纵 " + header + " 表时出现异常。");
throw e;
}
}
/**
* 插入纪录
* @param header
* @param body
*/
public void pushItemToDatabase(String header, String[] body) throws Exception {
String sql = null;
if (header.equalsIgnoreCase("info")) { //不处理
return;
}
else if (header.equalsIgnoreCase("fs")) {
pushSpecItemToDatabase(header, body, 5); //5 = 数据库字段个数
return;
}
else if (header.equalsIgnoreCase("osprocess")) {
pushSpecItemToDatabase(header, body, 11);
return;
}
else if (header.equalsIgnoreCase("diskio")) {
pushSpecItemToDatabase(header, body, 6);
return;
}
else if (header.equalsIgnoreCase("sybaselog")) {
pushSybaseItemsToDatabase(header, body, 2);
return;
}
else if (header.equalsIgnoreCase("sybasedata")) {
pushSybaseItemsToDatabase(header, body, 2);
return;
}
else if (header.equalsIgnoreCase("sybtran")) {
pushSybaseItemsToDatabase(header, body, 13);
return;
}
else if (header.equalsIgnoreCase("cicslogspace")) {
pushSpecItemToDatabase(header, body, 3);
return;
}
else if (header.equalsIgnoreCase("cicsfs")) {
pushSpecItemToDatabase(header, body, 2);
return;
}
else if (header.equalsIgnoreCase("cicsregions")) {
pushSpecItemToDatabase(header, body, 2);
return;
}
else if (header.equalsIgnoreCase("cicsregspar")) {
pushSpecItemToDatabase(header, body, 8);
return;
}
else if (header.equalsIgnoreCase("regionerror")) {
pushSpecItemToDatabase(header, body, 3);
return;
}
else if (header.equalsIgnoreCase("snasession")) {
pushSpecItemToDatabase(header, body, 4);
return;
}
else if (header.equalsIgnoreCase("sfserror")) {
pushSpecItemToDatabase(header, body, 3);
return;
}
else if (header.equalsIgnoreCase("snals")) {
pushSpecItemToDatabase(header, body, 2);
return;
}
else {
//单行纪录,包括cpu,memory,pg,engine,sybconnect,,snasession
try {
String fields = "";
for (int i = 0; i < body.length; i++) {
if (i == 0) {
fields += body[i].substring(0, body[i].indexOf("="));
}
else {
fields += "," + body[i].substring(0, body[i].indexOf("="));
}
}
fields += ",timestamp,ipaddress";
sql = "insert into " + header + "(" + fields + ") values ( ";
for (int i = 0; i < body.length + 2; i++) {
if (i == 0) {
sql += "?";
}
else {
sql += ",?";
}
}
sql += ")";
pstmt = con.prepareStatement(sql);
int i = 1;
for (; i < body.length + 1; i++) {
pstmt.setString(i, findValue(body[i - 1]));
}
pstmt.setTimestamp(i, timestamp);
pstmt.setString(i + 1, getIpAddress());
pstmt.executeUpdate();
pstmt.close();
}
catch (Exception e) {
System.err.println("数据库操作失败!在操纵 " + header + " 表时出现异常。");
e.printStackTrace();
}
}
}
public static void main(String[] args) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -