host.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding:utf-8 -*-
  2. __author__ = 'Ulric Qin'
  3. from .bean import Bean
  4. from rrd.store import db
  5. class Host(Bean):
  6. _tbl = 'host'
  7. _cols = 'id, hostname, maintain_begin, maintain_end'
  8. def __init__(self, _id, hostname, maintain_begin, maintain_end):
  9. self.id = _id
  10. self.hostname = hostname
  11. self.maintain_begin = maintain_begin
  12. self.maintain_end = maintain_end
  13. @classmethod
  14. def query(cls, page, limit, query, maintaining, group_id):
  15. where = 'id in (select host_id from grp_host where grp_id = %s)'
  16. params = [group_id]
  17. if maintaining == '1':
  18. where += ' and maintain_begin > 0 and maintain_end > 0'
  19. if query:
  20. where += ' and hostname like %s'
  21. params.append('%' + query + '%')
  22. vs = cls.select_vs(where=where, params=params, page=page, limit=limit, order='hostname')
  23. total = cls.total(where, params)
  24. return vs, total
  25. @classmethod
  26. def maintain(cls, begin, end, host_ids):
  27. if not host_ids:
  28. return 'host ids is blank'
  29. cls.update('maintain_begin = %s, maintain_end = %s where id in (%s)' % (begin, end, host_ids))
  30. return ''
  31. @classmethod
  32. def no_maintain(cls, host_ids):
  33. if not host_ids:
  34. return 'host ids is blank'
  35. cls.update('maintain_begin = 0, maintain_end = 0 where id in (%s)' % host_ids)
  36. return ''
  37. @classmethod
  38. def all_host_dict(cls):
  39. rows = db.query_all('SELECT id, hostname FROM host')
  40. ret = {}
  41. if rows:
  42. for row in rows:
  43. ret[row[0]] = row[1]
  44. return ret
  45. @classmethod
  46. def add(cls, host_id, hostname):
  47. if cls.exists('id=%s', [host_id]):
  48. return
  49. cls.insert({'id': host_id, 'hostname': hostname})
  50. @classmethod
  51. def create(cls, hostname):
  52. if cls.exists('hostname=%s', [hostname]):
  53. return
  54. cls.insert({'hostname': hostname})