Skip to content

Instantly share code, notes, and snippets.

@aeppert
Created March 2, 2016 21:37
Show Gist options
  • Select an option

  • Save aeppert/66ea894562d8f8b481c9 to your computer and use it in GitHub Desktop.

Select an option

Save aeppert/66ea894562d8f8b481c9 to your computer and use it in GitHub Desktop.
Annotate an Influxdb database with a PCAP being played for correlation across test events
#!/usr/bin/python
import sys
from optparse import OptionParser
import ConfigParser
from influxdb import InfluxDBClient
G_VALID_STATUS = ['start', 'stop']
def annotate(filename, status, configp):
# Load Influxdb Configuration
_host = configp.get('influxdb', 'host')
_port = configp.getint('influxdb', 'port')
_user = configp.get('influxdb', 'user')
_pass = configp.get('influxdb', 'pass')
_dbname = configp.get('influxdb', 'dbname')
annotation_body = [
{
"measurement": "events",
"tags": {
"type": "pcap",
"status": "start".format(status),
"text": "Playback of {}".format(filename)
},
"fields": {
"file": "{}".format(filename)
}
}
]
client = InfluxDBClient(_host, _port, _user, _pass, _dbname)
try:
client.create_database(_dbname)
except:
pass
client.switch_database(_dbname)
client.switch_user(_user, _pass)
client.write_points(annotation_body)
if __name__ == '__main__':
parser = OptionParser()
parser.add_option('--config', dest='config', help='Configuration')
parser.add_option('--file', dest='file', help='Filename to annotate the Influxdb replay_events database')
parser.add_option('--status', dest='status', help="Status of event - start or stop")
(options, args) = parser.parse_args()
if len(sys.argv) == 1 or len(options.config) == 0:
parser.print_help()
sys.exit(1)
configp = ConfigParser.ConfigParser()
configp.read(options.config)
if options.status not in G_VALID_STATUS:
print '{} is not a valid status - {}'.format(options.status, G_VALID_STATUS)
sys.exit(1)
annotate(options.file, options.status, configp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment