#!/usr/bin/env python

import os
import dtnstats
from munin import MuninPlugin

class DtnBundlesPlugin(MuninPlugin):
    title = "Bundles"
    args = "--base 1000 -l 0"
    vlabel = "bundles"
    scale = False
    category = "dtn"
    host = "localhost"
    port = 4550

    @property
    def fields(self):
        return [
            ("bundle_received", dict(
                label = "received",
                info = 'The bundles received by the daemon.',
                type = "COUNTER",
                min = "0")
            ),
            ("bundle_transmitted", dict(
                label = "transmitted",
                info = 'The bundles transmitted successfully to another peer.',
                type = "COUNTER",
                min = "0")
            ),
            ("bundle_aborted", dict(
                label = "aborted",
                info = 'Aborted transmissions of bundles.',
                type = "COUNTER",
                min = "0")
            ),
            ("bundle_requeued", dict(
                label = "requeued",
                info = 'Requeued transmissions.',
                type = "COUNTER",
                min = "0")
            ),
            ("bundle_expired", dict(
                label = "expired",
                info = 'The number of expired bundles.',
                type = "COUNTER",
                min = "0")
            ),
            ("bundle_queued", dict(
                label = "queued",
                info = 'The bundles queued for further processing.',
                type = "COUNTER",
                min = "0")
            )
        ]

    def execute(self):
        stats = dtnstats.DtnStats(self.host, self.port)
        stats.connect()
        data = stats.bundles()
        return dict(
            bundle_received=data['Received'],
            bundle_transmitted=data['Transmitted'],
            bundle_aborted=data['Aborted'],
            bundle_requeued=data['Requeued'],
            bundle_expired=data['Expired'],
            bundle_queued=data['Queued']
        )

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

