def set_vertex_attr(node): data = { 'ogit/_id': node['ogit/_id'], 'ogit/_type': node['ogit/_type'], args['--attr']: None } GraphitNode(session, data).update() print >> sys.stdout, "Attribute '{att}' of node '{id}' deleted".format( att=args['--attr'], id=node['ogit/_id'])
def delete_node(node): try: GraphitNode(session, {'ogit/_id': node}).delete() print >> sys.stderr, "Deleted {id}".format(id=node) except GraphitNodeError as e: print >> sys.stderr, e except GraphitError as e: print >> sys.stderr, "Failed to delete node {nodeid}: {e}".format( nodeid=node, e=e)
def set_vertex_attr(node): data = { 'ogit/_id': node['ogit/_id'], 'ogit/_type': node['ogit/_type'], args['--attr']: args['--value'] } GraphitNode(session, data).update() print >> sys.stdout, "Attribute '{att}' of node '{id}' set to '{val}'".format( att=args['--attr'], id=node['ogit/_id'], val=args['--value'])
def delete_if_orphan(node): q2 = VerbQuery(node['ogit/_id'], "ogit/corresponds") no_conn = len(list(session.query(q2, fields=['ogit/_id']))) if no_conn == 0: print >> sys.stdout, "{node} has {no} corresponding vertices, deleting ...".format( node=node['ogit/_id'], no=no_conn) GraphitNode( session, { 'ogit/_id': node['ogit/_id'], 'ogit/_type': 'ogit/ConfigurationItem' }).delete() else: print >> sys.stdout, "{node} has {no} corresponding vertices.".format( node=node['ogit/_id'], no=no_conn)
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
def delete_node(node): try: if args['--del-ci']: q2 = VerbQuery(node, "ogit/corresponds", ogit_types=['ogit/ConfigurationItem']) for item in session.query(q2, fields=['ogit/_id']): print >> sys.stderr, "Deleted corresponding ogit/ConfigurationItem {id}".format( id=item['ogit/_id']) GraphitNode( session, { 'ogit/_id': item['ogit/_id'], 'ogit/_type': 'ogit/ConfigurationItem' }).delete() MARSNode(session, { 'ogit/_id': node, 'ogit/_type': 'ogit/Automation/MARSNode' }).delete() print >> sys.stderr, "Deleted {id}".format(id=node) except GraphitNodeError as e: print >> sys.stderr, e except GraphitError as e: print >> sys.stderr, "Failed to delete node {nodeid}: {e}".format( nodeid=node, e=e)
def print_event(node): q2 = VerbQuery(node['ogit/_id'], "ogit/corresponds") for item in session.query(q2, fields=args['--field']): print >> sys.stdout, GraphitNode( session, item).json(pretty_print=args['--pretty'])
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
if args['vertex'] and args['get'] and args['OGITID']: q = IDQuery(args['OGITID']) try: if args['--count']: for r in session.query(q, fields=['ogit/_id'], count=args['--count']): print >> sys.stdout, r sys.exit(0) else: if args['--list']: args['--field'] = ["ogit/_id"] for r in session.query(q, fields=args['--field']): if args['--list']: print >> sys.stdout, GraphitNode(session, r).data['ogit/_id'] else: print >> sys.stdout, GraphitNode( session, r).json(pretty_print=args['--pretty']) sys.exit(0) except GraphitError as e: print >> sys.stderr, "Cannot list nodes: {err}".format(err=e) sys.exit(5) except GraphitNodeError as e: print >> sys.stderr, e sys.exit(5) if args['vertex'] and args['del']: def delete_node(node): try: