1
0

host.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # -*- coding:utf-8 -*-
  2. # Copyright 2017 Xiaomi, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. __author__ = 'Ulric Qin'
  16. from .bean import Bean
  17. from rrd.store import db
  18. class Host(Bean):
  19. _tbl = 'host'
  20. _cols = 'id, hostname, maintain_begin, maintain_end'
  21. def __init__(self, _id, hostname, maintain_begin, maintain_end):
  22. self.id = _id
  23. self.hostname = hostname
  24. self.maintain_begin = maintain_begin
  25. self.maintain_end = maintain_end
  26. @classmethod
  27. def query(cls, page, limit, query, maintaining, group_id):
  28. where = 'id in (select host_id from grp_host where grp_id = %s)'
  29. params = [group_id]
  30. if maintaining == '1':
  31. where += ' and maintain_begin > 0 and maintain_end > 0'
  32. if query:
  33. where += ' and hostname like %s'
  34. params.append('%' + query + '%')
  35. vs = cls.select_vs(where=where, params=params, page=page, limit=limit, order='hostname')
  36. total = cls.total(where, params)
  37. return vs, total
  38. @classmethod
  39. def maintain(cls, begin, end, host_ids):
  40. if not host_ids:
  41. return 'host ids is blank'
  42. cls.update('maintain_begin = %s, maintain_end = %s where id in (%s)' % (begin, end, host_ids))
  43. return ''
  44. @classmethod
  45. def no_maintain(cls, host_ids):
  46. if not host_ids:
  47. return 'host ids is blank'
  48. cls.update('maintain_begin = 0, maintain_end = 0 where id in (%s)' % host_ids)
  49. return ''
  50. @classmethod
  51. def all_host_dict(cls):
  52. rows = db.query_all('SELECT id, hostname FROM host')
  53. ret = {}
  54. if rows:
  55. for row in rows:
  56. ret[row[0]] = row[1]
  57. return ret
  58. @classmethod
  59. def add(cls, host_id, hostname):
  60. if cls.exists('id=%s', [host_id]):
  61. return
  62. cls.insert({'id': host_id, 'hostname': hostname})
  63. @classmethod
  64. def create(cls, hostname):
  65. if cls.exists('hostname=%s', [hostname]):
  66. return
  67. cls.insert({'hostname': hostname})