def addLog(msg, device, level): log = Log() log.device_id = device log.level = level log.message = msg log.area = device.device_name log.save()
def do_add(request): # # find the server # s = Server.objects(uuid=request.POST['server']).first() # # add the log # j = simplejson.loads(request.POST['log']) # # formatting the log # log = "" for l in j['line']: log += l.strip() + "\n" l = Log(server=s, log=j['log'], line=log) l.save()
def show_group(request, group=None): g = ServerGroup.objects(slug=group).first() if request.GET.has_key('log'): out = [] line = "" if request.method == "POST": # # grabbing the last 50 logs from our search string # logs = Log.objects(server__in=g.servers, log=request.GET['log'], line__icontains=request.POST['search']).limit(50) else: # # grabbing the last 50 logs # logs = Log.objects(server__in=g.servers, log=request.GET['log']).limit(50) if logs.count() > 0: old = logs[0] for l in logs: if old.server == l.server: line += l.line else: if old is not None: out.append({'server': old.server, 'line': line}) old = l line = l.line # # append the last line # if line != "": out.append({'server': old.server, 'line': line}) return render_to_response('server/log_group.html', {'group': g, 'logs': out}, context_instance=RequestContext(request)) return render_to_response('server/group.html', {'group': g}, context_instance=RequestContext(request))
def show_server(request, server=None): s = Server.objects(host=server).first() if request.GET.has_key('log'): if request.method == "POST": # # grabbing the last 50 logs from our search string # logs = Log.objects(server=s, log=request.GET['log'], line__icontains=request.POST['search']).limit(50) else: # # grabbing the last 50 logs # logs = Log.objects(server=s, log=request.GET['log']).limit(50) log = "" for l in logs: log += l.line return render_to_response('server/log.html', {'server': s, 'log': log}, context_instance=RequestContext(request)) return render_to_response('server/server.html', {'server': s}, context_instance=RequestContext(request))
perm = Permission( user=User.objects.get(pk=p['user_id']), tool=Tool.objects.get(pk=p['tool_id']), permission = int(p['permission']), addedby = User.objects.get(pk=check_added_by(p)), date = date ) perm.save() except ObjectDoesNotExist, e: print p print 'Warning: User (or possibly a tool) does not exist, did you import the carddb first? (%s)' % (e) # raise CommandError() for l in logs: # skip if it's not the tool we want. if onlytool: if l['tool_id'] != onlytool: continue # {u'tool_id': 1, u'logged_at': u'2013-05-16T19:57:59', u'user_id': 38, u'logged_event': u'Access Finished', u'time': 0} try: date = datetime.datetime.strptime(l['logged_at'], format) date = gmt.localize(date) l = Log(tool=Tool.objects.get(pk=l['tool_id']), user=User.objects.get(pk=l['user_id']), date=date, message=l['logged_event'], time=l['time']) l.save() except ObjectDoesNotExist, e: print "failed to add log line: %s" % (l) fh.close()
def handle(self, *args, **options): path = options['path'] if not os.path.exists(path): raise CommandError('Can\'t find %s' % (path)) # you can just import a single tool from the json file # good for just importing the 3-in-1 lathe from babbage for example onlytool = None if 'toolid' in options: try: onlytool = options['toolid'] except Exception as e: raise CommandError('not a tool id? %s : %s' % (options['toolid'], e)) fh = open(path, 'r') j = json.load(fh) if len(j) != 3: raise CommandError( 'The json file should have 3 top level items, not %d' % (len(j))) tools = j[0] perms = j[1] logs = j[2] for t in tools: # {u'status': 1, u'status_message': u'OK', u'acnode_id': 1, u'name': u'Three in One'} if onlytool: if t['acnode_id'] != onlytool: continue tool = Tool(name=t['name'], id=t['acnode_id'], status=t['status'], status_message=t['status_message']) tool.save() # format for importing dates. format = "%Y-%m-%dT%H:%M:%S" # the timestamps comes from a mysql # timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP # column, which uses the system timezone, which on babbage and the acserver is: # TZ='Europe/London' gmt = pytz.timezone('Europe/London') def check_added_by(p, out): added_by = None if p['added_by_user_id'] == None: # no user added this permission :/ # lets just use user 1 (Russ), it's as good as any out.write( "Warning: no added_by for permission %s, using user id 1" % (str(p))) added_by = 1 elif p['added_by_user_id'] == 0: out.write( "Warning: added_by for permission %s was 0, using user id 1" % (str(p))) added_by = 1 else: added_by = p['added_by_user_id'] return added_by for p in perms: # {u'last_used': None, u'user_id': 38, u'tool_id': 1, u'permission': 2, u'added_by_user_id': None, u'added_on': u'2013-05-05T02:38:47'} # we ignore last_used... # print p # skip if it's not the tool we want. if onlytool: if p['tool_id'] != onlytool: continue # check for existing permissions try: ep = Permission.objects.filter(user=User.objects.get( pk=p['user_id'])).get(tool_id=p['tool_id']) # ok, a permission already exists. # check in case it's been changed if ep.permission != int(p['permission']): self.stdout.write("permission changed!") self.stdout.write(str(ep)) self.stdout.write(str(p)) ep.permission = int(p['permission']) ep.addedby = User.objects.get( pk=check_added_by(p, self.stdout)) date = datetime.datetime.strptime(p['added_on'], format) ep.date = gmt.localize(date) ep.save() continue except ObjectDoesNotExist as e: # fine if it's not already in there. pass try: if not p['added_on']: date = timezone.now() else: date = datetime.datetime.strptime(p['added_on'], format) date = gmt.localize(date) perm = Permission(user=User.objects.get(pk=p['user_id']), tool=Tool.objects.get(pk=p['tool_id']), permission=int(p['permission']), addedby=User.objects.get( pk=check_added_by(p, self.stdout)), date=date) perm.save() except ObjectDoesNotExist as e: self.stdout.write(str(p)) self.stdout.write( 'Warning: User (or possibly a tool) does not exist, did you import the carddb first? (%s)' % (e)) # raise CommandError() for l in logs: # skip if it's not the tool we want. if onlytool: if l['tool_id'] != onlytool: continue # {u'tool_id': 1, u'logged_at': u'2013-05-16T19:57:59', u'user_id': 38, u'logged_event': u'Access Finished', u'time': 0} try: date = datetime.datetime.strptime(l['logged_at'], format) date = gmt.localize(date) l = Log(tool=Tool.objects.get(pk=l['tool_id']), user=User.objects.get(pk=l['user_id']), date=date, message=l['logged_event'], time=l['time']) l.save() except ObjectDoesNotExist as e: self.stdout.write("failed to add log line: %s" % (l)) fh.close()