var now = new Date;
var o=db.{{project.logCollection}}.findOne({"className":"org.log4mongo.contrib.JvmMonitor",timestamp:{$gt:new Date(now - 3*60*1000)}});
if(o){
result = o.message.match(".*memoryUsed=(\\d+).*cpuUsed=(\\d+).*threadCount=(\\d+)");
var memoryUsed = result[1]/1024;
var cpuUsed = result[2];
var threadCount = result[3];
db.{{project.metricCollection}}.save({name:"memoryUsed (M)",value:memoryUsed,timeStamp:now.getTime()});
db.{{project.metricCollection}}.save({name:"cpuUsed",value:cpuUsed,timeStamp:now.getTime()});
db.{{project.metricCollection}}.save({name:"threadCount",value:threadCount,timeStamp:now.getTime()});
return "cpuUsed="+ cpuUsed +" memoryUsed="+memoryUsed +" threadCount="+threadCount;
}
return "无记录";
//统计日志里某些数字总值,如每5分钟收入等
m=function () {
result = this.message.match(".*money=(\\d+)");
if (result) {
pricePaied = new NumberLong(result[1]);
emit("pricePaied", pricePaied);
}
}
r= function (key, values) {
var total = 0;
for (var i = 0; i < values.length; i++) {
total += values[i];
}
return total;
}
res=db.{{project.logCollection}}.mapReduce(m, r, {out:"{{project.logCollection}}_output", query:{timestamp:{$gt:new Date(new Date - 300000)}}});
pricePaied=db.{{project.logCollection}}_output.findOne({_id:"pricePaied"});
if(pricePaied)
v=pricePaied.value;
else
v=0;
db.{{project.metricCollection}}.save({name:"5分钟收入",value:v,timeStamp:new Date().getTime()});
return res;
//统计日志里某些关键字频率,并提取到告警邮件里
var metric_name="异常数据次数";
var cur = db.{{project.logCollection}}.find({message:/系统错误/,timestamp:{$gt:new Date(new Date - 60*60*1000)}});
var content = "";
var count=0;
cur.forEach( function(log) {
count++;
content =content+"\
"+log.timestamp.toLocaleDateString()+" "+ log.timestamp.toLocaleTimeString() +" " +log["message"];
});
db.{{project.metricCollection}}.save({name:metric_name,value:count, content:content,timeStamp:new Date().getTime()});
return "次数:"+ count +" 内容"+content;
//统计错误日志占比
err=db.{{project.logCollection}}.find({'level':'ERROR',timestamp:{$gt:new Date(new Date() - 300000)}}).count()
total=db.{{project.logCollection}}.find({timestamp:{$gt:new Date(new Date() - 300000)}}).count();
if(total==0) total=1;
value=err*100/total;
db.{{project.metricCollection}}.save({name:'错误日志百分比',value:value,timeStamp:new Date().getTime()});
return value;