collection.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # -*- coding: utf-8 -*-
  2. # Description:
  3. # Author: Ilya Mashchenko (ilyam8)
  4. # SPDX-License-Identifier: GPL-3.0-or-later
  5. import os
  6. from threading import Lock
  7. PATH = os.getenv('PATH', '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin').split(':')
  8. CHART_BEGIN = 'BEGIN {0} {1}\n'
  9. CHART_CREATE = "CHART {0} '{1}' '{2}' '{3}' '{4}' '{5}' {6} {7} {8}\n"
  10. DIMENSION_CREATE = "DIMENSION '{0}' '{1}' {2} {3} {4} '{5}'\n"
  11. DIMENSION_SET = "SET '{0}' = {1}\n"
  12. print_lock = Lock()
  13. def setdefault_values(config, base_dict):
  14. for key, value in base_dict.items():
  15. config.setdefault(key, value)
  16. return config
  17. def run_and_exit(func):
  18. def wrapper(*args, **kwargs):
  19. func(*args, **kwargs)
  20. exit(1)
  21. return wrapper
  22. def on_try_except_finally(on_except=(None,), on_finally=(None,)):
  23. except_func = on_except[0]
  24. finally_func = on_finally[0]
  25. def decorator(func):
  26. def wrapper(*args, **kwargs):
  27. try:
  28. func(*args, **kwargs)
  29. except Exception:
  30. if except_func:
  31. except_func(*on_except[1:])
  32. finally:
  33. if finally_func:
  34. finally_func(*on_finally[1:])
  35. return wrapper
  36. return decorator
  37. def static_vars(**kwargs):
  38. def decorate(func):
  39. for k in kwargs:
  40. setattr(func, k, kwargs[k])
  41. return func
  42. return decorate
  43. @on_try_except_finally(on_except=(exit, 1))
  44. def safe_print(*msg):
  45. """
  46. :param msg:
  47. :return:
  48. """
  49. print_lock.acquire()
  50. print(''.join(msg))
  51. print_lock.release()
  52. def find_binary(binary):
  53. """
  54. :param binary: <str>
  55. :return:
  56. """
  57. for directory in PATH:
  58. binary_name = os.path.join(directory, binary)
  59. if os.path.isfile(binary_name) and os.access(binary_name, os.X_OK):
  60. return binary_name
  61. return None
  62. def read_last_line(f):
  63. with open(f, 'rb') as opened:
  64. opened.seek(-2, 2)
  65. while opened.read(1) != b'\n':
  66. opened.seek(-2, 1)
  67. if opened.tell() == 0:
  68. break
  69. result = opened.readline()
  70. return result.decode()
  71. def unicode_str(arg):
  72. """Return the argument as a unicode string.
  73. The `unicode` function has been removed from Python3 and `str` takes its
  74. place. This function is a helper which will try using Python 2's `unicode`
  75. and if it doesn't exist, assume we're using Python 3 and use `str`.
  76. :param arg:
  77. :return: <str>
  78. """
  79. # TODO: fix
  80. try:
  81. # https://github.com/netdata/netdata/issues/7613
  82. if isinstance(arg, unicode):
  83. return arg
  84. return unicode(arg, errors='ignore')
  85. # https://github.com/netdata/netdata/issues/7642
  86. except TypeError:
  87. return unicode(arg)
  88. except NameError:
  89. return str(arg)