本文整理汇总了Python中utils.platform.Platform类的典型用法代码示例。如果您正苦于以下问题:Python Platform类的具体用法?Python Platform怎么用?Python Platform使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Platform类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, agentConfig):
try:
self.config_store = get_config_store(agentConfig=agentConfig)
except Exception as e:
log.error('Failed to instantiate the config store client. '
'Auto-config only will be used. %s' % str(e))
agentConfig['sd_config_backend'] = None
self.config_store = get_config_store(agentConfig=agentConfig)
self.dockerutil = DockerUtil(config_store=self.config_store)
self.docker_client = self.dockerutil.client
if Platform.is_k8s():
try:
self.kubeutil = KubeUtil()
except Exception as ex:
self.kubeutil = None
log.error("Couldn't instantiate the kubernetes client, "
"subsequent kubernetes calls will fail as well. Error: %s" % str(ex))
if Platform.is_nomad():
self.nomadutil = NomadUtil()
elif Platform.is_ecs_instance():
self.ecsutil = ECSUtil()
self.VAR_MAPPING = {
'host': self._get_host_address,
'port': self._get_port,
'tags': self._get_additional_tags,
}
AbstractSDBackend.__init__(self, agentConfig)
示例2: get_tags
def get_tags(self, state, c_id):
"""Extract useful tags from docker or platform APIs. These are collected by default."""
tags = []
if Platform.is_k8s():
pod_metadata = state.get_kube_config(c_id, 'metadata')
if pod_metadata is None:
log.warning("Failed to fetch pod metadata for container %s."
" Kubernetes tags may be missing." % c_id[:12])
return []
# get labels
kube_labels = pod_metadata.get('labels', {})
for label, value in kube_labels.iteritems():
tags.append('%s:%s' % (label, value))
# get replication controller
created_by = json.loads(pod_metadata.get('annotations', {}).get('kubernetes.io/created-by', '{}'))
if created_by.get('reference', {}).get('kind') == 'ReplicationController':
tags.append('kube_replication_controller:%s' % created_by.get('reference', {}).get('name'))
# get kubernetes namespace
tags.append('kube_namespace:%s' % pod_metadata.get('namespace'))
elif Platform.is_swarm():
c_labels = state.inspect_container(c_id).get('Labels', {})
swarm_svc = c_labels.get(DockerUtil.SWARM_SVC_LABEL)
if swarm_svc:
tags.append('swarm_service:%s' % c_labels)
return tags
示例3: _get_host_address
def _get_host_address(self, state, c_id, tpl_var):
"""Extract the container IP from a docker inspect object, or the kubelet API."""
c_inspect = state.inspect_container(c_id)
c_id = c_inspect.get('Id', '')
c_img = self.dockerutil.image_name_extractor(c_inspect)
networks = c_inspect.get('NetworkSettings', {}).get('Networks') or {}
ip_dict = {}
for net_name, net_desc in networks.iteritems():
ip = net_desc.get('IPAddress')
if ip:
ip_dict[net_name] = ip
ip_addr = self._extract_ip_from_networks(ip_dict, tpl_var)
if ip_addr:
return ip_addr
if Platform.is_k8s():
# kubernetes case
log.debug("Couldn't find the IP address for container %s (%s), "
"using the kubernetes way." % (c_id[:12], c_img))
pod_ip = state.get_kube_config(c_id, 'status').get('podIP')
if pod_ip:
return pod_ip
if Platform.is_rancher():
# try to get the rancher IP address
log.debug("No IP address was found in container %s (%s) "
"trying with the Rancher label" % (c_id[:12], c_img))
ip_addr = c_inspect.get('Config', {}).get('Labels', {}).get(RANCHER_CONTAINER_IP)
if ip_addr:
return ip_addr.split('/')[0]
log.error("No IP address was found for container %s (%s)" % (c_id[:12], c_img))
return None
示例4: get_config_path
def get_config_path(cfg_path=None, os_name=None):
# Check if there's an override and if it exists
if cfg_path is not None and os.path.exists(cfg_path):
return cfg_path
# Check if there's a config stored in the current agent directory
try:
path = os.path.realpath(__file__)
path = os.path.dirname(path)
return _config_path(path)
except PathNotFound as e:
pass
# Check for an OS-specific path, continue on not-found exceptions
bad_path = ''
try:
if Platform.is_windows():
common_data = _windows_commondata_path()
return _config_path(os.path.join(common_data, 'Datadog'))
elif Platform.is_mac():
return _config_path(MAC_CONFIG_PATH)
else:
return _config_path(UNIX_CONFIG_PATH)
except PathNotFound as e:
if len(e.args) > 0:
bad_path = e.args[0]
# If all searches fail, exit the agent with an error
sys.stderr.write("Please supply a configuration file at %s or in the directory where "
"the Agent is currently deployed.\n" % bad_path)
sys.exit(3)
示例5: testNetwork
def testNetwork(self):
# FIXME: cx_state to true, but needs sysstat installed
config = """
init_config:
instances:
- collect_connection_state: false
excluded_interfaces:
- lo
- lo0
"""
check, instances = get_check('network', config)
check.check(instances[0])
check.get_metrics()
metric_names = [m[0] for m in check.aggregator.metrics]
assert 'system.net.bytes_rcvd' in metric_names
assert 'system.net.bytes_sent' in metric_names
if Platform.is_linux():
assert 'system.net.tcp.retrans_segs' in metric_names
assert 'system.net.tcp.in_segs' in metric_names
assert 'system.net.tcp.out_segs' in metric_names
elif Platform.is_bsd():
assert 'system.net.tcp.retrans_packs' in metric_names
assert 'system.net.tcp.sent_packs' in metric_names
assert 'system.net.tcp.rcv_packs' in metric_names
示例6: _get_checks_to_refresh
def _get_checks_to_refresh(self, state, c_id):
"""Get the list of checks applied to a container from the identifier_to_checks cache in the config store.
Use the DATADOG_ID label or the image."""
inspect = state.inspect_container(c_id)
# If the container was removed we can't tell which check is concerned
# so we have to reload everything.
# Same thing if it's stopped and we're on Kubernetes in auto_conf mode
# because the pod was deleted and its template could have been in the annotations.
if not inspect or \
(not inspect.get('State', {}).get('Running')
and Platform.is_k8s() and not self.agentConfig.get('sd_config_backend')):
self.reload_check_configs = True
return
identifier = inspect.get('Config', {}).get('Labels', {}).get(DATADOG_ID) or \
self.dockerutil.image_name_extractor(inspect)
platform_kwargs = {}
if Platform.is_k8s():
kube_metadata = state.get_kube_config(c_id, 'metadata') or {}
platform_kwargs = {
'kube_annotations': kube_metadata.get('annotations'),
'kube_container_name': state.get_kube_container_name(c_id),
}
return self.config_store.get_checks_to_refresh(identifier, **platform_kwargs)
示例7: __init__
def __init__(self, parent=None):
QMenu.__init__(self, parent)
self.options = {}
system_tray_menu = [
(self.START, lambda: agent_manager("start")),
(self.STOP, lambda: agent_manager("stop")),
(self.RESTART, lambda: agent_manager("restart")),
]
# First the version
self.addAction(self.ABOUT.format(get_version())).setEnabled(False)
self.addSeparator()
for name, action in system_tray_menu:
self.add_option(name, action)
# enable or disable mac login
if Platform.is_mac():
self.add_option(self.MAC_LOGIN.format(self.enable_or_disable_mac()),
lambda: self.enable_or_disable_login())
elif Platform.is_windows():
self.add_option(self.FLARE, lambda: thread.start_new_thread(windows_flare, ()))
# And finally the exit
self.add_option(self.EXIT, lambda: sys.exit(0))
self.connect(self, SIGNAL("aboutToShow()"), lambda: self.update_options())
示例8: initialize_logging
def initialize_logging(logger_name):
try:
logging_config = get_logging_config()
logging.basicConfig(
format=get_log_format(logger_name),
level=logging_config['log_level'] or logging.INFO,
)
log_file = logging_config.get('%s_log_file' % logger_name)
if log_file is not None and not logging_config['disable_file_logging']:
# make sure the log directory is writeable
# NOTE: the entire directory needs to be writable so that rotation works
if os.access(os.path.dirname(log_file), os.R_OK | os.W_OK):
file_handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=LOGGING_MAX_BYTES, backupCount=1)
formatter = logging.Formatter(get_log_format(logger_name), get_log_date_format())
file_handler.setFormatter(formatter)
root_log = logging.getLogger()
root_log.addHandler(file_handler)
else:
sys.stderr.write("Log file is unwritable: '%s'\n" % log_file)
# set up syslog
if logging_config['log_to_syslog']:
try:
from logging.handlers import SysLogHandler
if logging_config['syslog_host'] is not None and logging_config['syslog_port'] is not None:
sys_log_addr = (logging_config['syslog_host'], logging_config['syslog_port'])
else:
sys_log_addr = "/dev/log"
# Special-case BSDs
if Platform.is_darwin():
sys_log_addr = "/var/run/syslog"
elif Platform.is_freebsd():
sys_log_addr = "/var/run/log"
handler = SysLogHandler(address=sys_log_addr, facility=SysLogHandler.LOG_DAEMON)
handler.setFormatter(logging.Formatter(get_syslog_format(logger_name), get_log_date_format()))
root_log = logging.getLogger()
root_log.addHandler(handler)
except Exception, e:
sys.stderr.write("Error setting up syslog: '%s'\n" % str(e))
traceback.print_exc()
# Setting up logging in the event viewer for windows
if get_os() == 'windows' and logging_config['log_to_event_viewer']:
try:
from logging.handlers import NTEventLogHandler
nt_event_handler = NTEventLogHandler(logger_name, get_win32service_file('windows', 'win32service.pyd'), 'Application')
nt_event_handler.setFormatter(logging.Formatter(get_syslog_format(logger_name), get_log_date_format()))
nt_event_handler.setLevel(logging.ERROR)
app_log = logging.getLogger(logger_name)
app_log.addHandler(nt_event_handler)
except Exception, e:
sys.stderr.write("Error setting up Event viewer logging: '%s'\n" % str(e))
traceback.print_exc()
示例9: test_check
def test_check(self):
config = {'instances': self.MYSQL_CONFIG}
self.run_check_twice(config)
# Test service check
self.assertServiceCheck('mysql.can_connect', status=AgentCheck.OK,
tags=self.SC_TAGS, count=1)
# Travis MySQL not running replication - FIX in flavored test.
self.assertServiceCheck('mysql.replication.slave_running', status=AgentCheck.CRITICAL,
tags=self.SC_TAGS, count=1)
ver = map(lambda x: int(x), self.service_metadata[0]['version'].split("."))
ver = tuple(ver)
testable_metrics = (self.STATUS_VARS + self.VARIABLES_VARS + self.INNODB_VARS
+ self.BINLOG_VARS + self.SYSTEM_METRICS + self.SCHEMA_VARS + self.SYNTHETIC_VARS)
if ver >= (5, 6, 0):
testable_metrics.extend(self.PERFORMANCE_VARS)
# Test metrics
for mname in testable_metrics:
# These two are currently not guaranteed outside of a Linux
# environment.
if mname == 'mysql.performance.user_time' and not Platform.is_linux():
continue
if mname == 'mysql.performance.kernel_time' and not Platform.is_linux():
continue
if mname == 'mysql.performance.cpu_time' and Platform.is_windows():
continue
if mname == 'mysql.performance.query_run_time.avg':
self.assertMetric(mname, tags=self.METRIC_TAGS+['schema:testdb'], count=1)
elif mname == 'mysql.info.schema.size':
self.assertMetric(mname, tags=self.METRIC_TAGS+['schema:testdb'], count=1)
self.assertMetric(mname, tags=self.METRIC_TAGS+['schema:information_schema'], count=1)
self.assertMetric(mname, tags=self.METRIC_TAGS+['schema:performance_schema'], count=1)
else:
self.assertMetric(mname, tags=self.METRIC_TAGS, count=1)
# Assert service metadata
self.assertServiceMetadata(['version'], count=1)
# test custom query metrics
self.assertMetric('alice.age', value=25)
self.assertMetric('bob.age', value=20)
# test optional metrics
self._test_optional_metrics((self.OPTIONAL_REPLICATION_METRICS
+ self.OPTIONAL_INNODB_VARS
+ self.OPTIONAL_STATUS_VARS
+ self.OPTIONAL_STATUS_VARS_5_6_6), 1)
# Raises when COVERAGE=true and coverage < 100%
self.coverage_report()
示例10: get_tags
def get_tags(self, state, c_id):
"""Extract useful tags from docker or platform APIs. These are collected by default."""
c_inspect = state.inspect_container(c_id)
tags = self.dockerutil.extract_container_tags(c_inspect)
if Platform.is_k8s():
pod_metadata = state.get_kube_config(c_id, 'metadata')
if pod_metadata is None:
log.warning("Failed to fetch pod metadata for container %s."
" Kubernetes tags may be missing." % c_id[:12])
return []
# get pod labels
kube_labels = pod_metadata.get('labels', {})
for label, value in kube_labels.iteritems():
tags.append('%s:%s' % (label, value))
# get kubernetes namespace
namespace = pod_metadata.get('namespace')
tags.append('kube_namespace:%s' % namespace)
# add creator tags
creator_tags = self.kubeutil.get_pod_creator_tags(pod_metadata)
tags.extend(creator_tags)
# add services tags
if self.kubeutil.collect_service_tag:
services = self.kubeutil.match_services_for_pod(pod_metadata)
for s in services:
if s is not None:
tags.append('kube_service:%s' % s)
elif Platform.is_swarm():
c_labels = c_inspect.get('Config', {}).get('Labels', {})
swarm_svc = c_labels.get(SWARM_SVC_LABEL)
if swarm_svc:
tags.append('swarm_service:%s' % swarm_svc)
elif Platform.is_rancher():
service_name = c_inspect.get('Config', {}).get('Labels', {}).get(RANCHER_SVC_NAME)
stack_name = c_inspect.get('Config', {}).get('Labels', {}).get(RANCHER_STACK_NAME)
container_name = c_inspect.get('Config', {}).get('Labels', {}).get(RANCHER_CONTAINER_NAME)
if service_name:
tags.append('rancher_service:%s' % service_name)
if stack_name:
tags.append('rancher_stack:%s' % stack_name)
if container_name:
tags.append('rancher_container:%s' % container_name)
if self.metadata_collector.has_detected():
orch_tags = self.metadata_collector.get_container_tags(co=c_inspect)
tags.extend(orch_tags)
return tags
示例11: _get_events
def _get_events(self):
"""Get the list of events."""
events, changed_container_ids = self.docker_util.get_events()
if not self._disable_net_metrics:
self._invalidate_network_mapping_cache(events)
if changed_container_ids and self._service_discovery:
get_sd_backend(self.agentConfig).update_checks(changed_container_ids)
if changed_container_ids:
self.metadata_collector.invalidate_cache(events)
if Platform.is_nomad():
self.nomadutil.invalidate_cache(events)
elif Platform.is_ecs_instance():
self.ecsutil.invalidate_cache(events)
return events
示例12: get_tags
def get_tags(self, state, c_id):
"""Extract useful tags from docker or platform APIs. These are collected by default."""
tags = []
if Platform.is_k8s():
pod_metadata = state.get_kube_config(c_id, 'metadata')
if pod_metadata is None:
log.warning("Failed to fetch pod metadata for container %s."
" Kubernetes tags may be missing." % c_id[:12])
return []
# get pod labels
kube_labels = pod_metadata.get('labels', {})
for label, value in kube_labels.iteritems():
tags.append('%s:%s' % (label, value))
# get kubernetes namespace
namespace = pod_metadata.get('namespace')
tags.append('kube_namespace:%s' % namespace)
# get created-by
created_by = json.loads(pod_metadata.get('annotations', {}).get('kubernetes.io/created-by', '{}'))
creator_kind = created_by.get('reference', {}).get('kind')
creator_name = created_by.get('reference', {}).get('name')
# add creator tags
if creator_name:
if creator_kind == 'ReplicationController':
tags.append('kube_replication_controller:%s' % creator_name)
elif creator_kind == 'DaemonSet':
tags.append('kube_daemon_set:%s' % creator_name)
elif creator_kind == 'ReplicaSet':
tags.append('kube_replica_set:%s' % creator_name)
else:
log.debug('creator-name for pod %s is empty, this should not happen' % pod_metadata.get('name'))
# FIXME haissam: for service and deployment we need to store a list of these guys
# that we query from the apiserver and to compare their selectors with the pod labels.
# For service it's straight forward.
# For deployment we only need to do it if the pod creator is a ReplicaSet.
# Details: https://kubernetes.io/docs/user-guide/deployments/#selector
elif Platform.is_swarm():
c_labels = state.inspect_container(c_id).get('Config', {}).get('Labels', {})
swarm_svc = c_labels.get(SWARM_SVC_LABEL)
if swarm_svc:
tags.append('swarm_service:%s' % swarm_svc)
return tags
示例13: _report_performance_metrics
def _report_performance_metrics(self, containers_by_id):
containers_without_proc_root = []
for container in containers_by_id.itervalues():
if self._is_container_excluded(container) or not self._is_container_running(container):
continue
tags = self._get_tags(container, PERFORMANCE)
try:
self._report_cgroup_metrics(container, tags)
if "_proc_root" not in container:
containers_without_proc_root.append(DockerUtil.container_name_extractor(container)[0])
continue
self._report_net_metrics(container, tags)
except BogusPIDException as e:
self.log.warning('Unable to report cgroup metrics: %s', e)
if containers_without_proc_root:
message = "Couldn't find pid directory for containers: {0}. They'll be missing network metrics".format(
", ".join(containers_without_proc_root))
if not Platform.is_k8s():
self.warning(message)
else:
# On kubernetes, this is kind of expected. Network metrics will be collected by the kubernetes integration anyway
self.log.debug(message)
示例14: _get_host_address
def _get_host_address(self, state, c_id, tpl_var):
"""Extract the container IP from a docker inspect object, or the kubelet API."""
c_inspect = state.inspect_container(c_id)
c_id, c_img = c_inspect.get('Id', ''), c_inspect.get('Config', {}).get('Image', '')
networks = c_inspect.get('NetworkSettings', {}).get('Networks') or {}
ip_dict = {}
for net_name, net_desc in networks.iteritems():
ip = net_desc.get('IPAddress')
if ip:
ip_dict[net_name] = ip
ip_addr = self._extract_ip_from_networks(ip_dict, tpl_var)
if ip_addr:
return ip_addr
# try to get the bridge (default) IP address
log.debug("No IP address was found in container %s (%s) "
"networks, trying with the IPAddress field" % (c_id[:12], c_img))
ip_addr = c_inspect.get('NetworkSettings', {}).get('IPAddress')
if ip_addr:
return ip_addr
if Platform.is_k8s():
# kubernetes case
log.debug("Couldn't find the IP address for container %s (%s), "
"using the kubernetes way." % (c_id[:12], c_img))
pod_ip = state.get_kube_config(c_id, 'status').get('podIP')
if pod_ip:
return pod_ip
log.error("No IP address was found for container %s (%s)" % (c_id[:12], c_img))
return None
示例15: _add_conf_tar
def _add_conf_tar(self):
conf_path = get_config_path()
if self._can_read(conf_path, output=False):
self._add_clean_conf(
conf_path,
'etc',
self.MAIN_CREDENTIALS
)
if not Platform.is_windows():
supervisor_path = os.path.join(
os.path.dirname(get_config_path()),
'supervisor.conf'
)
if self._can_read(supervisor_path, output=False):
self._add_clean_conf(
supervisor_path,
'etc'
)
for file_path in glob.glob(os.path.join(get_confd_path(), '*.yaml')) +\
glob.glob(os.path.join(get_confd_path(), '*.yaml.default')):
if self._can_read(file_path, output=False):
self._add_clean_conf(
file_path,
os.path.join('etc', 'confd'),
self.CHECK_CREDENTIALS
)