package org.zw.excel; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.ArrayList; import java.util.List; import org.apache.commons.net.telnet.TelnetClient; public class MachineTelnetInfo { private int time = 1500; private String IP = "10.17.7.137"; // telnet的IP地址 public MachineTelnetInfo(String iP) { super(); IP = iP; } private String port = "23"; // 端口号,默认23 private String user = "root";// 用户名 private List rowsInfo = null;// new ArrayList();// 解析出来的信息 public MachineTelnetInfo(String iP, String user, String pwd) { super(); IP = iP; this.user = user; this.pwd = pwd; } public MachineTelnetInfo() { super(); } private String pwd = "root88"; // 用户密码 //private char startTag = '[';// 系统标示符号 private char endTag = '#';// 系统标示符号 private TelnetClient tc = null; private BufferedReader in; // 输入流,接收返回信息 private PrintStream out; // 像 服务器写入 命令 public void connect() throws InterruptedException { try { tc = new TelnetClient(); tc.connect(IP, Integer.parseInt(port)); in = new BufferedReader(new InputStreamReader(tc.getInputStream())); // in = tc.getInputStream(); out = new PrintStream(tc.getOutputStream()); } catch (Exception e) { System.out.println("connect error !"); } System.out.println("服务器IP:"+IP+" success?" + tc.isConnected()); out.println(user); // 是发送到服务器上的请求信息 out.flush(); Thread.sleep(time); out.println(pwd); out.flush(); } public String excute(String command) throws InterruptedException, IOException { Thread.sleep(1500); out.println(command); out.flush(); int i = 0; StringBuffer sb = new StringBuffer(); try { char ch = (char) in.read(); while (true) { if (i == 1) sb.append(ch); if (ch == endTag) i++; if (i > 1) break; ch = (char) in.read(); } } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } public List getInfo() throws Exception { if(rowsInfo==null){//obtain this machine information by protocol of telnet this.connect(); String str = this.excute("df -g"); InputStream is = new ByteArrayInputStream(str.getBytes()); ParseMachineInfo it = new ParseMachineInfo(); rowsInfo = it.readToList(is); close(); } return rowsInfo; } public static void main(String[] args) throws Exception { MachineTelnetInfo tt = new MachineTelnetInfo("10.17.7.129"); List list = tt.getInfo(); for (int i = 0; i < list.size(); i++) { String[] strs =(String[])(list.get(i)); for (int j = 0; j < strs.length; j++) { System.out.print(strs[j]+"|"); } System.out.println(); } } public static void main1(String[] args) throws InterruptedException, IOException { MachineTelnetInfo tt = new MachineTelnetInfo(); tt.connect(); String str = tt.excute("df -g"); System.out.println("----------------finsh---------------------------"); //System.out.println(str); InputStream is = new ByteArrayInputStream(str.getBytes()); ParseMachineInfo it = new ParseMachineInfo(); tt.rowsInfo = it.readToList(is); for (int i = 0; i < tt.rowsInfo.size(); i++) { String[] strs =(String[])(tt.rowsInfo.get(i)); for (int j = 0; j < strs.length; j++) { System.out.print(strs[j]+"|"); } System.out.println(); } tt.close(); } public void close() { try { tc.disconnect(); in.close(); // out.close(); } catch (IOException e1) { e1.printStackTrace(); } } }
package org.zw.excel; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import jxl.Workbook; import jxl.format.Colour; import jxl.format.UnderlineStyle; import jxl.write.Label; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; public class ExcelView { private String[] widths; private String targetfile = "F:/out.xls ";// 输出的excel文件名 private String worksheet = "主机列表检查"; // 输出的excel文件工作表名 WritableSheet sheet = null;// 工作表 WritableCellFormat alarmrows = null; WritableCellFormat lightalarmrows = null; WritableCellFormat colsrow = null; WritableCellFormat headrow = null; private int row = 0 ; //String[] IPs = {"10.17.7.131","10.17.7.132"}; //private String[] machinenames = {"OSS_SERV1","OSS_SERV2"}; String[] IPs = {"10.17.7.131","10.17.7.132","10.17.7.137","10.17.7.138","10.17.7.139","10.17.7.140","10.17.7.141" ,"10.17.7.133","10.17.7.134","10.17.7.135","10.17.7.136","10.17.7.148","10.17.7.149"}; private String[] machinenames = {"OSS_SERV1","OSS_SERV2","OSS_WEB1","OSS_WEB2","OSS_COMM1","OSS_COMM2","Test" ,"NMS_SERV1","NMS_SERV2","NMS_APP1","NMS_APP2","cognosDB","TMS(root/linus123)"} ; Label label; WritableWorkbook workbook; String[] titles; public void init() throws WriteException, IOException{ WritableFont alarmfont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); alarmrows = new WritableCellFormat(alarmfont); alarmrows.setBackground(Colour.RED); lightalarmrows = new WritableCellFormat(alarmfont); lightalarmrows.setBackground(Colour.YELLOW2); WritableFont colsfont = new WritableFont(WritableFont.ARIAL, 11, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); colsrow = new WritableCellFormat(colsfont); colsrow.setBackground(Colour.GRAY_50); WritableFont _font = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); headrow = new WritableCellFormat(_font); headrow.setBackground(Colour.LIGHT_BLUE); Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); String _date = formatter.format(date) ; targetfile = "F:/主机检查_"+_date+".xls"; // 创建可写入的Excel工作薄,运行生成的文件在tomcat/bin下 System.out.println("begin"); OutputStream os = new FileOutputStream(targetfile); workbook = Workbook.createWorkbook(os); sheet = workbook.createSheet(worksheet, 0); // 添加第一个工作表 setColSpace(); Date date0 = new Date(); SimpleDateFormat _formatter = new SimpleDateFormat("yyyy-MM-dd"); String __date = _formatter.format(date0) ; //String[] _colums = { "Filesystem", "Mounted","GB_blocks", "Free","%Used",__date,"Remarks" }; // excel工作表的标题 String[] _colums = { "Filesystem", "GB_blocks", "Free","%Used","Iused","%Iused","Mounted",__date,"Remarks" }; // excel工作表的标题 titles = _colums; } private void setColSpace() { if(widths==null)return; for (int i = 0; i < widths.length; i++) { String[] strs = widths[i].split(","); //System.out.println(strs[0]+":"+strs[1]); sheet.setColumnView(Integer.parseInt(strs[0]), Integer.parseInt(strs[1])); } } public void setWidths(String[] widths) { this.widths = widths; } public static void main(String[] args) throws WriteException, IOException { ExcelView ev = new ExcelView(); String[] widths = { "0,17", "1,13","2,10","3,10", "4,12","5,10","6,18","7,14","8,30"}; ev.setWidths(widths); ev.init(); for (int i = 0; i < ev.IPs.length; i++){ try { ev.generateView(i); } catch (Exception e) { System.out.println("IP--"+ev.IPs[i]+"有错误"); continue; } ev.row = ev.row+3; } ev.workbook.write(); ev.workbook.close(); //---------------------------//-------------------------- Runtime r = Runtime.getRuntime(); Process p = null; String cmd[] = {"C:\\Program Files\\Microsoft Office\\Office12\\EXCEL.EXE",ev.targetfile }; try { p = r.exec(cmd); } catch (Exception e) { System.out.println("error executing: " + cmd[0]); } } /* //还有个功能要做,就是实现与上回的对比功能 public void generateView1(int server){ try { //head sheet.mergeCells(0, row, 3, row); label =new Label(0, row,machinenames[server]+"——"+IPs[server],headrow); sheet.addCell(label); row++; //columns for (int i = 0; i < titles.length; i++) { // Label(列号,行号 ,内容 ) label = new Label(i, row, titles[i],colsrow); // put the title in row1 sheet.addCell(label); } row++; //body int _row = row+7; while (row < _row) { // Label(列号,行号 ,内容 ) for (int _col = 0; _col < titles.length; _col++) { label = new Label(_col, row, row+"行"+_col+"列"); // put the title in row1 sheet.addCell(label); } if(row==5) for (int col = 0; col < titles.length; col++) { label = new Label(col, row, row+"行"+col+"列",alarmrows); // put the title in row1 sheet.addCell(label); } if(row==6) for (int col = 0; col < titles.length; col++) { label = new Label(col, row, "3.145"); // put the title in row1 sheet.addCell(label); } if(row==3) for (int col = 0; col < titles.length; col++) { label = new Label(col, row, "29.00%"); // put the title in row1 sheet.addCell(label); } row++; } } catch (Exception e) { e.printStackTrace(); } System.out.println("end"); }*/ //还有个功能要做,就是实现与上回的对比功能 public void generateView(int server){ try { //head sheet.mergeCells(0, row, 3, row); label =new Label(0, row,machinenames[server]+"——"+IPs[server],headrow); sheet.addCell(label); row++; //columns for (int i = 0; i < titles.length; i++) { // Label(列号,行号 ,内容 ) label = new Label(i, row, titles[i],colsrow); // put the title in row1 sheet.addCell(label); } row++; //body MachineTelnetInfo tt = null; if(IPs[server].equals("10.17.7.149"))tt = new MachineTelnetInfo(IPs[server],"root","linus123"); else tt = new MachineTelnetInfo(IPs[server]); List list = tt.getInfo(); for (int i = 0; i < list.size(); i++) { switch (isAlarm(getContent(list,i,3))) { case 0: for (int _col = 0; _col < titles.length; _col++) { if(_col<7){ label = new Label(_col, row, getContent(list,i,_col)); // put the title in row1 sheet.addCell(label);} else if(_col==7){ label = new Label(_col, row, " OK "); // put the title in row1 sheet.addCell(label); } else { label = new Label(_col, row, " "); // put the title in row1 sheet.addCell(label); } } break; case 1: for (int _col = 0; _col < titles.length; _col++) { if(_col<7){ label = new Label(_col, row, getContent(list,i,_col),lightalarmrows); // put the title in row1 sheet.addCell(label);} else if(_col==7){ label = new Label(_col, row, " OK ",lightalarmrows); // put the title in row1 sheet.addCell(label); } else { label = new Label(_col, row, " ",lightalarmrows); // put the title in row1 sheet.addCell(label); } } break; case 2: // Label(列号,行号 ,内容 ) for (int _col = 0; _col < titles.length; _col++) { if(_col<7){ label = new Label(_col, row, getContent(list,i,_col),alarmrows); // put the title in row1 sheet.addCell(label);} else if(_col==7){ label = new Label(_col, row, " XX ",alarmrows); // put the title in row1 sheet.addCell(label); } else { label = new Label(_col, row, " ",alarmrows); // put the title in row1 sheet.addCell(label); } } break; default: break; } row++; } } catch (Exception e) { e.printStackTrace(); } //System.out.println("end"); } public String getContent(List list,int row,int col){ String[] strs =(String[])list.get(row); String content = strs[col]; return content; } public int pivot = 80; public int isAlarm(String percent){ int level = 0;//0 normal 1 light_alarm 2 alarm int i = 0; try { i = Integer.parseInt(percent.substring(0, percent.indexOf('%'))); if(i>=60)level =1; if(i>=80)level = 2; } catch (Exception e) { return 0; } return level; } }