#!/usr/bin/env python
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken.  If not, see <http://www.gnu.org/licenses/>.

# This program transforms a flat dependency file into a json one so it can be loaded in
# hot_dependencies_arbiter module
# The input file format is :
# host1 : vm1
# host2 : vm2
# ...
# You can now get a live update of your dependency tree in shinken for your xen/virtualbox/qemu
# All you have to do is finding a way to modify this flat file when you do a live migration
# for example, you can use a script like this in your crontab
# dsh -Mc -g mydom0group 'xm list | awk "/vm-/ { print \$1 }"' > /tmp/shinken_flat_mapping
#

import os
import sys
import optparse

# Try to load json (2.5 and higer) or simplejson if failed (python2.4)
try:
    import json
except ImportError:
    # For old Python version, load
    # simple json (it can be hard json?! It's 2 functions guy!)
    try:
        import simplejson as json
    except ImportError:
        sys.exit("Error: you need the json or simplejson module for this script")

VERSION = '0.1'


def main(input_file, output_file):
    # Check if input_file is newer than output_file
    if os.path.exists(output_file):
        if os.path.getmtime(output_file) >= os.path.getmtime(input_file):
            print "Nothing to do"
            return True
    r = []
    flatmappingfile = open(input_file, 'rb')

    for ligne in flatmappingfile:
        ligne = ligne.rstrip('\n\r')
        parts = ligne.split(':')
        host = parts[0]
        vm = parts[1]
        v = (('host', host), ('host', vm))
        r.append(v)

    flatmappingfile.close()

    jsonmappingfile = open(output_file, 'wb')
    buf = json.dumps(r)
    jsonmappingfile.write(buf)
    jsonmappingfile.close()

if __name__ == "__main__":
    # Manage the options
    parser = optparse.OptionParser(
        version="Shinken external flat mapping file to json mapping %s" % VERSION)
    parser.add_option("-o", "--output", dest='output_file', default='/tmp/external_mapping_file.json',
                      help="Path of the generated json mapping file.")
    parser.add_option("-i", "--input", dest='input_file', default='/tmp/shinken_flat_mapping',
                      help="Path oh the flat mapping file")

    opts, args = parser.parse_args()
    if args:
        parser.error("does not take any positional arguments")

    main(**opts.__dict__)

@@ -692,18 +692,20 @@
 ses/%3E.%0A%0A
-# 
+%22%22%22%0A
 This pro
@@ -764,17 +764,17 @@
 ne so it
- 
+%0A
 can be l
@@ -781,18 +781,16 @@
 oaded in
-%0A#
  hot_dep
@@ -814,18 +814,17 @@
  module%0A
-# 
+%0A
 The inpu
@@ -843,47 +843,49 @@
 t is
- 
 :%0A
-#
+ 
  host1 
-:
+%22:%22
  vm1%0A
-#
+ 
  host2 
-:
+%22:%22
  vm2%0A
-#
+ 
  ...%0A
-# 
+%0A
 You 
@@ -948,17 +948,17 @@
 nken for
- 
+%0A
 your xen
@@ -973,18 +973,17 @@
 box/qemu
-%0A#
+.
  All you
@@ -1013,17 +1013,17 @@
 a way to
- 
+%0A
 modify t
@@ -1068,12 +1068,12 @@
 tion
-%0A# f
+.%0A%0AF
 or e
@@ -1126,18 +1126,21 @@
  crontab
-%0A#
+::%0A%0A 
  dsh -Mc
@@ -1167,10 +1167,19 @@
 list
+'
  %7C
+ %5C%0A     
  awk
@@ -1230,17 +1230,21 @@
 mapping%0A
-#
+%0A%22%22%22%0A
 %0A%0Aimport

Move description into a doc-string.