Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Node.js script that aggregates Jenkins Build Failure Analyzer plugin statistics from MongoDB to Graphite, known and unknown failures per hour.
/*
The MIT License
Copyright 2014 Sony Mobile Communications AB. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
//A Node.js script that aggregates Jenkins Build Failure Analyzer plugin
//statistics from MongoDB to Graphite, known and unknown failures per hour
var MongoClient = require('mongodb').MongoClient;
var net = require("net");
var utils = require("./utils.js"); //TODO Implement your own, see below
MongoClient.connect('mongodb://mongo.mycompany.net:27017/jenkinsbfa', function(err, db) { //TODO Change server
if(err) throw err;
var collection = db.collection('statistics');
var now = new Date();
var then = new Date();
//From three months back, since rescans can happen
then.setMonth(then.getMonth() - 3);
var match = {"$match": {
"startingTime": {"$gte": then},
"result": {"$ne": "ABORTED"}
}
};
var project = { "$project": {
"startingTime": 1,
"master": 1,
"failureCauses": {"$ifNull": ["$failureCauses", false]}
}
};
var group = { "$group": {
"_id": {
"hour": {"$hour": "$startingTime"},
"dayOfMonth": {"$dayOfMonth": "$startingTime"},
"month": {"$month": "$startingTime"},
"year": {"$year": "$startingTime"},
"master": "$master",
"isNullFailureCause": {"$eq": ["$failureCauses", false]},
},
"number": {"$sum": 1}
}
};
var graphitePostfixKnown = ".bfa.known.hourly";
var graphitePostfixUnKnown = ".bfa.unknown.hourly";
var socket = net.createConnection(2003, "graphite.mycompany.net"); //TODO change server name
socket.on('connect', function() {
console.log("Socket connected, starting aggregation...");
collection.aggregate([match, project, group], function(err, result) {
if (err !== null) {
socket.end();
throw err;
}
for(i = 0; i < result.length; i++) {
var item = result[i];
var id = item._id;
var time = new Date(id.year, id.month-1, id.dayOfMonth, id.hour, 0, 0, 0);
var master = id.master;
var group = utils.bfaMaster2GraphiteGroup(master); //TODO implement map from jenkins master url to graphite node
if (group != null) {
if (id.isNullFailureCause) {
var line = group + graphitePostfixUnKnown + " " + item.number + " " + (time.getTime() / 1000) + "\n";
socket.write(line);
console.log(line);
} else {
var line = group + graphitePostfixKnown + " " + item.number + " " + (time.getTime() / 1000) + "\n";
socket.write(line);
console.log(line);
}
}
}
// Let's close the db
db.close();
//And the socket
socket.end();
console.log("Done");
});
}).on("error", function(exception) {
console.log("Error connecting to graphite: " + exception);
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment