import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class GetDetail {
private static String ipName = "";
public static void main(String[] args) {
try {
getIp();
cycle1();
cycle2();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void getIp()throws Exception{
String str = "";
String eth = "";
Process process1 = Runtime.getRuntime().exec("mii-tool");
InputStreamReader reader1 = new InputStreamReader(process1
.getInputStream());
LineNumberReader line1 = new LineNumberReader(reader1);
while ((str = line1.readLine()) != null) {
if (str.contains("link ok")) {
eth = str.substring(0, str.indexOf(":"));
break;
}
}
Process process = Runtime.getRuntime().exec("ifconfig " + eth);
InputStreamReader reader = new InputStreamReader(process
.getInputStream());
LineNumberReader line = new LineNumberReader(reader);
int i = 0;
String[] strs = null;
String[] childStrs = null;
while ((str = line.readLine()) != null) {
i++;
if (i == 2) {
strs = str.split("\\s+");
childStrs = strs[2].split(":");
ipName = childStrs[1];
}
}
System.out.println(ipName);
}
public static void cycle1() throws Exception {
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
try {
Connection conn = getConn();
PreparedStatement pst = null;
Double totalUsedCpuRate = 0.0;//the rate of used CPU
Double totalUsedMemRate = 0.0;//the rate of used memory
Double totalUsedMem = 0.0;//total used memory
Double totalMem =0.0;//total memory
Double totalFreeMem = 0.0;//total free memory
Double totalBufferMem = 0.0;//total buffer memory
String totalTask = "";//total task
String runningTask = "";//the number of running task
String sleepingTask = "";//the number of sleeping task
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("top -b -n 1");
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = "";
int linecount = 0;
while ((str = in.readLine()) != null) {
linecount++;
if (linecount ==2){
String[] s = str.split("\\s+");
totalTask = s[1];
runningTask = s[3];
sleepingTask = s[5];
}
if (linecount ==3) {
String[] s = str.split("\\s+");
String[] cpustr = s[1].split("%");
totalUsedCpuRate=Double.parseDouble(cpustr[0]);
}
if (linecount ==4) {
String[] s = str.split("\\s+");
String[] memstr={s[1].substring(0,s[1].length()-1),s[3].substring(0,s[3].length()-1),s[5].substring(0,s[5].length()-1),s[7].substring(0,s[7].length()-1)};
totalMem = Double.parseDouble(memstr[0]);
totalUsedMem = Double.parseDouble(memstr[1]);
totalFreeMem = Double.parseDouble(memstr[2]);
totalBufferMem = Double.parseDouble(memstr[3]);
totalUsedMemRate=(Double.parseDouble(memstr[1])/Double.parseDouble(memstr[0]))*100;
}
}
String sql = "insert into t_Jvm(ipName,totalUsedMem,totalMem,totalFreeMem,totalBufferMem,time,comparedTime,totalUsedCpuRate,totalUsedMemRate,totalTask,runningTask,sleepingTask) values('"
+ ipName
+ "',"
+ totalUsedMem+","+totalMem+","+totalFreeMem+","+totalBufferMem
+ ",'"
+ new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date())
+ "',"
+ System.currentTimeMillis()
+ ","
+ totalUsedCpuRate + "," + totalUsedMemRate+ ",'" + totalTask+ "','" + runningTask+ "','" + sleepingTask+ "')";
System.out.println(sql);
pst = conn.prepareStatement(sql);
pst.execute();
pst.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, 1000);
}
public static void cycle2() throws Exception {
final Timer timer2 = new Timer();
timer2.schedule(new TimerTask() {
public void run() {
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("top -b -n 1");
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = "";
int linecount = 0;
long currentTime = System.currentTimeMillis();
Connection conn = getConn();
String sql="insert into t_client(ip,pid,user,pr,ni,virt,res,shr,status,cpu,mem,time,command,currentTime) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
conn.setAutoCommit(false);
while ((str = in.readLine()) != null&&(str = in.readLine()) != "") {
linecount++;
if (linecount >= 8) {
if(str!=null){
String[] s = str.split("\\s+");
List strs = new ArrayList();
for(int i=0;i
if(!s[i].equals("")){
strs.add(s[i]);
}
}
if(strs.size()!=0){
pstmt.setString(1, ipName);
pstmt.setString(2, strs.get(0));
pstmt.setString(3, strs.get(1));
pstmt.setString(4, strs.get(2));
pstmt.setString(5, strs.get(3));
pstmt.setString(6, strs.get(4));
pstmt.setString(7, strs.get(5));
pstmt.setString(8, strs.get(6));
pstmt.setString(9, strs.get(7));
pstmt.setString(10, strs.get(8));
pstmt.setString(11, strs.get(9));
pstmt.setString(12, strs.get(10));
pstmt.setString(13, strs.get(11));
pstmt.setLong(14, currentTime);
pstmt.addBatch();//批处理
}
}
}
}
pstmt.executeBatch();//执行批处理
pstmt.close();
conn.commit();//提交
conn.close();//关闭数据库
} catch (Exception e) {
e.printStackTrace();
}
}
},0,1000);
}
public static Connection getConn() {
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String connStr = "jdbc:mysql://192.168.20.58:3306/wljk?useUnicode=true&characterEncoding=utf-8";
String userName = "root";
String password = "1234";
conn = DriverManager.getConnection(connStr,userName,password);
}catch(Exception ex){
System.out.println(ex);
}
return conn;
}
}