#!/usr/bin/env python

import os, sys
from munin.mongodb import MuninMongoDBPlugin

class MonqueJobsPendingPlugin(MuninMongoDBPlugin):
    args = "-l 0 --base 1000"
    vlabel = "jobs"
    title = "Monque jobs pending"
    info = "Operations"
    category = "Monque"

    @property
    def fields(self):
        queues = [x.split(':', 2)[-1] for x in self.db.collection_names() if x.startswith('monque:queues:')]
        return [
            (queue+"_pending", dict(
                label = "%s jobs pending" % queue,
                info = "%s jobs pending" % queue,
                type = "GAUGE",
                min = "0",
                warning = "100",
                critical = "200"
            )) for queue in queues
        ]

    def execute(self):
        result = dict()
        queues = [x.split(':', 2)[-1] for x in self.db.collection_names() if x.startswith('monque:queues:')]
        for name in queues:
            result["%s_pending" % name] = self.db["monque:queues:"+name].count()
        return result

if __name__ == "__main__":
    MonqueJobsPendingPlugin().run()
