Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ansible callback example 1. create directory "callback_plugin" 2. place one of this file. that's all
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import datetime
format = "%Y-%m-%d %H:%M:%S"
class CallbackModule(object):
def __init__(self):
self.stats = []
def playbook_on_play_start(self, pattern):
self.starttime = datetime.datetime.now()
def playbook_on_task_start(self, name, is_conditional):
now = datetime.datetime.now()
self.current = {"name": name, "start": now}
def runner_on_ok(self, host, res):
now = datetime.datetime.now()
self.current['end'] = now
self.current['elapsed'] = now - self.current['start']
self.stats.append(self.current)
def playbook_on_stats(self, stats):
now = datetime.datetime.now()
print(",".join(["名前","開始時刻","終了時刻", "実行時間"]))
# Print the timings
for s in self.stats:
diff = s['elapsed'].total_seconds()
print(",".join([s['name'].encode('utf-8'),
s['start'].strftime(format),
s['end'].strftime(format),
"{0:.02f}".format(diff)
]))
diff = (now - self.starttime).total_seconds()
print(",".join(["Playbook全体",
now.strftime(format),
self.starttime.strftime(format),
"{0:.02f}".format(diff)]))
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pip install influxdb
# and conf.ini is
#
# [default]
# host = influxdb.example.com
# port = 8086
# user = playbook
# password = demo
#
#
import os
import time
import datetime
import json
import urllib
import urllib2
from ConfigParser import SafeConfigParser
from influxdb import InfluxDBClient
format = "%Y-%m-%d %H:%M:%S"
confpath = "conf.ini"
class CallbackModule(object):
def __init__(self):
self.stats = []
def playbook_on_play_start(self, pattern):
self.starttime = datetime.datetime.now()
def playbook_on_task_start(self, name, is_conditional):
now = datetime.datetime.now()
self.current = {"name": name, "start": now}
def runner_on_ok(self, host, res):
now = datetime.datetime.now()
self.current['end'] = now
self.current['elapsed'] = now - self.current['start']
self.stats.append(self.current)
def playbook_on_stats(self, stats):
now = datetime.datetime.now()
diff = (now - self.starttime).total_seconds()
self.post(diff)
def post(self, diff):
parser = SafeConfigParser()
parser.read(os.path.join(os.path.dirname(__file__), confpath))
host = parser.get("default", "host")
port = parser.get("default", "port")
user = parser.get("default", "user")
password = parser.get("default", "password")
dbname = "playbook"
data = [
{
"name": "playbook",
"columns": ["elapsed"],
"points": [[diff]]
}
]
client = InfluxDBClient(host, port, user, password, dbname)
client.write_points(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment