public class SearchData
{
private static final Logger logger = Logger.getLogger(SearchData.class);
//static String SMTP_HOST = "";
//static String SMTP_HOST = "";
static String SMTP_FROM_USER = "";
static String SUBJECT = "exception summary";
static String separator=File.separator;
static int total=0;
static String[] properties=null;
static Map<String,MyInteger> criticalityMap=new HashMap<String,MyInteger>();
@SuppressWarnings("static-access")
public static void main(String[] args)
{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMddHHmmss");
URL url=SearchData.class.getResource("/");
String currentPath=url.getPath();
int i=currentPath.indexOf("class");
currentPath=currentPath.substring(0,i-1);
System.out.println(currentPath+separator+"log4j.properties");
PropertyConfigurator.configure(currentPath+separator+"log4j.properties");
//prod value
int interval = Integer.valueOf(args[0]) * 60;
String sendTo = args[1];
String clusterName = args[2];
String type = args[3];
String hostname = args[4];
int port = Integer.valueOf(args[5]);
String appCode=args[6].toUpperCase();
String smtp_host=args[7];
Object object=new Object();
logger.info("interval: "+interval+";sendTo: "+sendTo+";clusterName: "+clusterName+";type: "+type);
logger.info("hostname: "+hostname+";port: "+port+";appCode: "+appCode);
//DEV test
/*int interval = 2*60;
String sendTo = "";
String clusterName = "logsearch";
String type = "applog";
String hostname = "";
int port = 9300;
String appCode="CDS";
String content = "";*/
// SimpleDateFormat df=new
// SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
// //2014-06-12T08:18:02.793Z
// df.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(currentPath);
String excelPath=null;
String from = df.format(getTime());
String now = null;
String subject=appCode+" exception summary";
while (true)
{
System.out.println("from: " + from);
logger.info("start time: "+from);
try
{
excelPath=currentPath+separator+"log"+separator+df1.format(df.parse(from))+".xls";
}
catch (ParseException e1)
{
logger.error(e1.getStackTrace());
}
System.out.println(excelPath);
try
{
Thread.currentThread().sleep(interval * 1000); //sleep interval seconds
}
catch (InterruptedException e)
{
logger.error(e.getStackTrace());
}
now = df.format(getTime());
System.out.println("to: " + now);
logger.info("end time: "+now);
FilterBuilder filter = FilterBuilders.andFilter(FilterBuilders.rangeFilter("@timestamp").gte(from).to(now)); //filter for query based on time
Builder settingsBuilder = ImmutableSettings.settingsBuilder();
settingsBuilder.put("cluster.name", clusterName);
settingsBuilder.put("client.transport.sniff", true);
TransportClient client = new TransportClient(settingsBuilder);
client.addTransportAddresses(new InetSocketTransportAddress(hostname, port));
SearchResponse response = client.prepareSearch().setTypes(type).setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setPostFilter(filter)
.setSize(1000)
.setQuery(QueryBuilders.matchQuery("Appcode", appCode))
.execute().actionGet();
SearchHit[] hits = response.getHits().getHits();
client.close();
if (hits.length != 0)
{
synchronized(object)
{
genSummaryFile(excelPath, hits);
sendMail(sendTo,smtp_host, getMessageBody(appCode, from, now, total), excelPath,subject);
criticalityMap.clear();
total = 0;
}
}
from = now;
}
}
//get America time
static Date getTime()
{
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
return calendar.getTime();
}
static String getMessageBody(String appCode,String from,String to,int total){
StringBuffer sb=new StringBuffer("The below is the summary of the applog exception. For detail, please see the attached file."+"\n\n");
sb.append("AppCode: "+appCode+"\n");
sb.append("From Time: "+from+"\n");
sb.append("To Time: "+to+"\n");
Iterator<String> it=criticalityMap.keySet().iterator();
while(it.hasNext()){
String critical=it.next();
int value=criticalityMap.get(critical).getValue();
sb.append(critical+": "+value+"\n");
}
sb.append("Total: "+total+"\n");
sb.append("\n"+"Please don't reply this mail!");
return sb.toString();
}
static void sendMail(String tos, String smtp_host, String mailBody,String attachedFile, String subject)
{
Properties props = new Properties();
props.put("mail.smtp.host", smtp_host);
props.setProperty("mail.transport.protocol", "smtp");
//System.out.println(props.getProperty("mail.smtp.host"));
Session session = Session.getInstance(props, null);
try
{
MimeMessage msg = new MimeMessage(session);
msg.setFrom(SMTP_FROM_USER);
msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(tos));
msg.setSubject(subject);
msg.setSentDate(new Date());
// Create a multipar message
Multipart multipart = new MimeMultipart();
// msg.setText(content);
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText(mailBody);
//messageBodyPart.setContent(arg0, arg1)
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
BodyPart attacheFilePart = new MimeBodyPart();
DataSource source = new FileDataSource(attachedFile);
attacheFilePart.setDataHandler(new DataHandler(source));
String fileName=attachedFile.substring(attachedFile.lastIndexOf(separator)+1,attachedFile.length());
attacheFilePart.setFileName(fileName);
multipart.addBodyPart(attacheFilePart);
msg.setContent(multipart);
Transport.send(msg);
}
catch (MessagingException mex)
{
System.out.println("send failed, exception: " + mex);
logger.error("send failed, exception: " + mex);
}
}
static String[] parseSource(String source){
String[] src=source.split("\",\""); //split source by ","
String[] values=new String[src.length];
for(int i=0;i<src.length;i++){
values[i]=src[i].split("\":\"")[1]; //split source by ":"
}
return values;
}
static void genSummaryFile(String file,SearchHit[] hits){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Log Exception Sunmmary");
CellStyle dateStyle=workbook.createCellStyle();
DataFormat format=workbook.createDataFormat();
dateStyle.setDataFormat(format.getFormat("yyyy-MM-dd:HH:mm:ss"));
CellStyle stringStyle=workbook.createCellStyle();
stringStyle.setWrapText(true);
sheet.setColumnWidth(0, 2300);
sheet.setColumnWidth(1, 5000);
sheet.setColumnWidth(2, 5000);
sheet.setColumnWidth(3, 8000);
sheet.setColumnWidth(4, 8000);
sheet.setColumnWidth(5, 6000);
sheet.setColumnWidth(6, 15000);
Object[] header=new Object[] {"Appcode","Timestamp","HostName","Criticality","LogFileName","Pattern","Message"};
ArrayList<Object[]> contents=new ArrayList<Object[]>();
contents.add(header);
for(SearchHit searchHit:hits){
String source = searchHit.getSourceAsString();
String[] values=parseSource(source);
String criticality=values[2];
if(criticalityMap.containsKey(criticality)){
criticalityMap.get(criticality).plus(1);
}else {
criticalityMap.put(criticality, new MyInteger(1));
}
total++;
Object[] r=null;
try
{
r=new Object[]{values[0],df.parse(values[4]),values[1],
values[2],values[3],values[6].replace("\"}", ""),values[5]};
}
catch (ParseException e)
{
e.printStackTrace();
logger.error(e.getStackTrace());
}
contents.add(r);
}
int rownum = 0;
for (Object[] objArr : contents) {
Row row = sheet.createRow(rownum++);
int cellnum = 0;
for (Object obj : objArr) {
Cell cell = row.createCell(cellnum++);
if(obj instanceof Date){
cell.setCellStyle(dateStyle);
cell.setCellValue((Date)obj);
}
else if(obj instanceof String){
cell.setCellStyle(stringStyle);
cell.setCellValue((String)obj);
}
}
}
try {
FileOutputStream out =
new FileOutputStream(new File(file));
workbook.write(out);
out.close();
logger.info("file path: "+file);
System.out.println("Excel written successfully..");
logger.info("Excel written successfully..");
} catch (FileNotFoundException e) {
e.printStackTrace();
logger.error(e.getStackTrace());
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getStackTrace());
}
}
}
class MyInteger{
private int value;
public MyInteger(int value)
{
this.value=value;
}
public void plus(int increment){
value+=increment;
}
public int getValue(){
return value;
}
}