Beispiel #1
0
 def create_missing_ci(mars_id):
     try:
         mars_node = MARSNode.from_graph(session, mars_id)
     except GraphitError as e:
         if e.status == 404:
             print >> sys.stdout, "{id}: Failure, node does not exists.".format(
                 id=mars_id)
         else:
             print >> sys.stdout, "{id}: Failure: {err}".format(
                 id=mars_id, err=e)
         return
     new_id = args['--value']
     q = EESQuery([
         "+ogit/id:\"{new_id}\"".format(new_id=new_id),
         "+ogit/_type:\"ogit/ConfigurationItem\""
     ])
     if len(list(session.query(q, fields=['ogit/_id']))) > 0:
         print "{id}: ConfigurationItem with ogit/id '{c}' already exists".format(
             id=mars_id, c=new_id)
         return
     data = {
         "ogit/_owner": mars_node.get_attr('ogit/_owner'),
         "ogit/_type": "ogit/ConfigurationItem",
         "ogit/ciType": mars_node.get_attr('/NodeType'),
         "ogit/id": new_id,
         "ogit/name": new_id
     }
     try:
         ci_node = GraphitNode(session, data)
         ci_node.push()
         mars_node.connect('ogit/corresponds', ci_node)
         print >> sys.stdout, mars_id + ": ConfigurationItem created: " + ci_node.data[
             'ogit/_id']
         return
     except GraphitError as e:
         print >> sys.stderr, e
Beispiel #2
0
 def create_missing_ci(mars_id):
     try:
         mars_node = MARSNode.from_graph(session, mars_id)
         #print mars_node.json(pretty_print=True)
     except GraphitError as e:
         if e.status == 404:
             print >> sys.stdout, "{id}: Failure, node does not exists.".format(
                 id=mars_id)
         else:
             print >> sys.stdout, "{id}: Failure: {err}".format(
                 id=mars_id, err=e)
         return
     if (args['--attr'] == '_SHORTNAME_'
             and mars_node.get_attr('/NodeType') != 'Machine'):
         print >> sys.stdout, mars_id + ": _SHORTNAME_ not applicable, works only with machine nodes."
         return
     elif (args['--attr'] == '_SHORTNAME_'
           and mars_node.get_attr('/NodeType') == 'Machine' and
           not hostname_regex.match(mars_node.get_attr('/NodeName'))):
         print >> sys.stdout, "{id}: _SHORTNAME_ not applicable, '{nn}' is not a valid FQDN'".format(
             id=mars_id, nn=mars_node.get_attr('/NodeName'))
         return
     elif (args['--attr'] == '_SHORTNAME_'
           and mars_node.get_attr('/NodeType') == 'Machine'
           and hostname_regex.match(mars_node.get_attr('/NodeName'))):
         new_id = mars_node.get_attr('/NodeName').split('.', 1)[0]
     elif mars_node.get_attr(args['--attr']):
         try:
             new_id = json.loads(mars_node.get_attr(
                 args['--attr']))['items'][0][0]
         except ValueError:
             new_id = mars_node.get_attr(args['--attr'])
     else:
         print >> sys.stdout, "{id}: Failure, node doesn't have an attribute '{attr}'".format(
             id=mars_id, attr=args['--attr'])
         return
     q = EESQuery([
         "+ogit/id:\"{new_id}\"".format(new_id=new_id),
         "+ogit/_type:\"ogit/ConfigurationItem\""
     ])
     if len(list(session.query(q, fields=['ogit/_id']))) > 0:
         print "{id}: ConfigurationItem with ogit/id '{c}' already exists".format(
             id=mars_id, c=new_id)
         return
     data = {
         "ogit/_owner": mars_node.get_attr('ogit/_owner'),
         "ogit/_type": "ogit/ConfigurationItem",
         "ogit/ciType": mars_node.get_attr('/NodeType'),
         "ogit/id": new_id,
         "ogit/name": new_id
     }
     try:
         ci_node = GraphitNode(session, data)
         #print(ci_node.json(pretty_print=True))
         ci_node.push()
         mars_node.connect('ogit/corresponds', ci_node)
         print >> sys.stdout, mars_id + ": ConfigurationItem created: " + ci_node.data[
             'ogit/_id']
         return
     except GraphitError as e:
         print >> sys.stderr, e