提交 2cb56a53 编写于 作者: J jialinsun

代码风格完善

上级 8892ebf2
package com.dianping.cat.agent.monitor.puppet;
public class Alteration {
@Override
public String toString() {
return "Alteration [type=" + type + ", ip=" + ip + ", hostname=" + hostname + ", user=" + user + ", url=" + url
+ ", op=" + op + ", date=" + date + ", title=" + title + ", content=" + content + ", domain=" + domain
+ ", group=" + group + "]";
}
private String type;
private String m_type;
private String ip;
private String m_ip;
private String hostname;
private String m_hostname;
private String user;
private String m_user;
private String url;
private String m_url;
private String op;
private String m_op;
private String date;
private String m_date;
private String title;
private String m_title;
private String content;
private String m_content;
private String domain;
private String m_domain;
private String group;
private String m_group;
public String getType() {
return type;
return m_type;
}
public void setType(String type) {
this.type = type;
m_type = type;
}
public String getIp() {
return ip;
return m_ip;
}
public void setIp(String ip) {
this.ip = ip;
m_ip = ip;
}
public String getHostname() {
return m_hostname;
}
public void setHostname(String hostname) {
m_hostname = hostname;
}
public String getUser() {
return user;
return m_user;
}
public void setUser(String user) {
this.user = user;
m_user = user;
}
public String getUrl() {
return url;
return m_url;
}
public void setUrl(String url) {
this.url = url;
m_url = url;
}
public String getOp() {
return op;
return m_op;
}
public void setOp(String op) {
this.op = op;
m_op = op;
}
public String getDate() {
return date;
return m_date;
}
public void setDate(String date) {
this.date = date;
m_date = date;
}
public String getTitle() {
return title;
return m_title;
}
public void setTitle(String title) {
this.title = title;
m_title = title;
}
public String getContent() {
return content;
return m_content;
}
public void setContent(String content) {
this.content = content;
m_content = content;
}
public String getDomain() {
return domain;
return m_domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
m_domain = domain;
}
public String getGroup() {
return group;
return m_group;
}
public void setGroup(String group) {
this.group = group;
m_group = group;
}
}
package com.dianping.cat.agent.puppet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HttpPostUtils {
public static String httpPost(String urlAddress,Map<String, String> paramMap){
if(paramMap==null){
paramMap = new HashMap<String, String>();
}
String [] params = new String[paramMap.size()];
int i = 0;
for(String paramKey:paramMap.keySet()){
String param = paramKey+"="+paramMap.get(paramMap);
params[i] = param;
i++;
}
return httpPost(urlAddress, params);
}
public static String httpPost(String urlAddress,List<String> paramList){
if(paramList==null){
paramList = new ArrayList<String>();
}
return httpPost(urlAddress, paramList.toArray(new String[0]));
}
public static String httpPost(String urlAddress,String []params){
URL url = null;
HttpURLConnection con =null;
BufferedReader in = null;
StringBuffer result = new StringBuffer();
try {
url = new URL(urlAddress);
con = (HttpURLConnection) url.openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
con.setRequestMethod("POST");
String paramsTemp = "";
for(String param:params){
if(param!=null&&!"".equals(param.trim())){
paramsTemp+="&"+param;
}
}
byte[] b = paramsTemp.getBytes();
con.getOutputStream().write(b, 0, b.length);
con.getOutputStream().flush();
con.getOutputStream().close();
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
while (true) {
String line = in.readLine();
if (line == null) {
break;
}
else {
result.append(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(in!=null){
in.close();
}
if(con!=null){
con.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result.toString();
}
}
package com.dianping.cat.agent.puppet;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.regex.*;
public class PuppetMonitorByLineNo {
public static void main(String[] args) {
String log_file = "/var/log/messages";
String line_file = "/var/log/currentPointer";
File line_f = new File(line_file);
while (true) {
int line_no_start = get_startline_no(line_file, log_file);
int line_reading_count = 0;
String regEx = ".*puppet-agent.*\\(\\/Stage";
System.out.println("line_no_start=" + Integer.toString(line_no_start));
try {
FileReader reader = new FileReader(log_file);
BufferedReader br = new BufferedReader(reader);
String str = null;
while ((str = br.readLine()) != null) {
line_reading_count++;
if (line_reading_count < line_no_start) {
continue;
} else {
Pattern pattern1 = Pattern.compile(".*?\\[(.*?)\\].*?");
if (Pattern.compile(regEx).matcher(str).find()) {
// System.out.println(str+"/n");
String[] tmp_list = str.split(" ");
String action_time = tmp_list[0] + tmp_list[1] + " " + tmp_list[2] + " " + tmp_list[3];
String hostname = tmp_list[4];
String change = str.split("\\(")[1];
String file = change.split("\\)")[0].split("Stage\\[main\\]")[1];
Matcher matcher1 = pattern1.matcher(file);
if (matcher1.matches()) {
System.out.println(matcher1.group(1));
}
System.out.println(action_time + " " + hostname + " " + change + "/n");
}
}
}
br.close();
reader.close();
System.out.println("本次读取到文件行数:" + line_reading_count);
// 将上次读取的行,写入文件
BufferedWriter output = new BufferedWriter(new FileWriter(line_f));
output.write(Integer.toString(line_reading_count));
output.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// slee 5s
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
*
* @param line_file
* 记录上次读取文件的行数
* @param log_file
* 读取文件,用于分析内容,此处用来统计文件总行数
* @return 返回上次读取的文件的行数
*/
public static int get_startline_no(String line_file, String log_file) {
int line_no_start = 0;
String str = "";
try {
FileReader line_reader = new FileReader(line_file);
BufferedReader line_br = new BufferedReader(line_reader);
try {
if ((str = line_br.readLine()) != null) {
line_no_start = Integer.parseInt(str);
line_br.close();
line_reader.close();
} else {// file is empty
line_no_start = countFileLine(log_file) / 2;
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
line_no_start = countFileLine(log_file) / 2;
File f = new File(line_file);
try {
if (f.createNewFile()) {
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(Integer.toString(line_no_start) + "\n");
output.close();
} else {
System.out.println("fail to create file : " + line_file);
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
System.out.println("上次读取的文件行数" + Integer.toString(line_no_start));
return line_no_start;
}
/**
*
* @param file
* 文件
* @return 文件的总行数
*/
public static int countFileLine(String file) {
int count = 0;
try {
InputStream is = new BufferedInputStream(new FileInputStream(file));
byte[] c = new byte[65536];
int readChars = 0;
while ((readChars = is.read(c)) != -1) {
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n')
++count;
}
}
is.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("文件总行数" + Integer.toString(count));
return count;
}
}
package com.dianping.cat.agent.puppet;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class Test {
public static void main(String args[]){
String m_logFile = "/var/log/messages";
RandomAccessFile reader=null;
try {
reader = new RandomAccessFile(m_logFile, "r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
System.out.println(reader.length());
} catch (IOException e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册