def test_basic(self): id0 = uuid4().hex id1 = uuid4().hex sinfo = model.SInfo(id=id0, name='service a') sinfo.save() sinfo.id = id1 sinfo.name = 'service b' sinfo.save() infos = model.SInfo.query(orderby='name ASC') self.assertEqual(2, len(infos)) self.assertEqual(id0, infos[0].id) self.assertEqual(id1, infos[1].id)
def _agent_smetrics(self, msg): aid = msg.agentid collect_at = msg.collect_at body = msg.body sname = body['name'] spid = body['pid'] stype = body.get('type', None) smetrics = [ model.SMetric(aid, collect_at, sname, spid, mname, mcontent, datetime.utcnow()) for mname, mcontent in body['metrics'].items() ] model.SMetric.save_all(smetrics) # calculate node services services = {s.name: s for s in model.SInfo.query_by_aid(aid)} if sname not in services: # service discovered logging.info('service %s discovered with pid %s', sname, spid) ser = model.SInfo(id=uuid4().hex, aid=aid, name=sname, pid=spid, type=stype, last_report_at=collect_at, status=model.SInfo.STATUS_ACT).save() ser.add_history(collect_at) else: # existing service, check for an update ser = services[sname] logging.debug('refreshing service %s', ser) ser.set(last_report_at=collect_at, status=model.SInfo.STATUS_ACT) if ser.pid != int(spid): logging.info('service [%s] pid change detected: %s -> %s', sname, ser.pid, spid) ser.chgpid(spid, collect_at) # set service to inactive if no status update for 5 minutes for sname, service in services.items(): active = service.chkstatus(300) # 300 seconds if not active: logging.info('service %s turn to inactive.', service) self._parse_smetrics(smetrics, ser) return True
def test_chgpid(self): ct = datetime.now() id = uuid4().hex sinfo = model.SInfo(id=id, aid='1', name='serv', pid=123, last_report_at=datetime.now()) sinfo.save() self.assertEqual('1', sinfo.aid) self.assertEqual('serv', sinfo.name) sinfo.chgpid(456, ct) sinfo1 = sinfo.query_by_aid('1')[0] self.assertEqual(sinfo, sinfo1) self.assertEqual(456, sinfo1.pid) history = model.SInfoHistory.query() self.assertEqual(1, len(history)) self.assertEqual(ct, history[0].collect_at)
def test_chkstatus_invaliddate(self): d = datetime.now() + timedelta(seconds=100) sinfo = model.SInfo(last_report_at=d, status=model.SInfo.STATUS_INACT) self.assertTrue(sinfo.chkstatus(200)) self.assertTrue(sinfo.chkstatus(99)) self.assertEqual(model.SInfo.STATUS_ACT, sinfo.status)
def test_chkstatus(self): d1 = datetime.utcnow() - timedelta(seconds=100) sinfo = model.SInfo(last_report_at=d1, status=model.SInfo.STATUS_INACT) self.assertTrue(sinfo.chkstatus(200)) self.assertFalse(sinfo.chkstatus(99)) self.assertEqual(model.SInfo.STATUS_INACT, sinfo.status)