Exemplo n.º 1
0
    def get_stats(self, instances, is_paas=False):
        self.attrs = self.data
        item = self.data['item']
        zabbix_db_client = ZabbixDBClient()
        if is_paas and item == 'memory_util':
            item = 'memory_util_agent'
        if is_paas and item == 'cpu_util':
            item = 'cpu_util_agent'
        item_stats = zabbix_db_client.get_item_stats(
            instances, item, self.data['start_timestamp'], self.data['end_timestamp'], self.data['segments_count'])
        # XXX: Quick and dirty fix: zabbix presents percentage of free space(not utilized) for storage
        if self.data['item'] in ('storage_root_util', 'storage_data_util'):
            for stat in item_stats:
                if 'value' in stat:
                    stat['value'] = 100 - stat['value']

        # XXX: temporary hack: show zero as value if one of the instances was created less then 30 minutes ago and
        # actual value is not available
        def is_instance_newly_created(instance):
            return instance.created > timezone.now() - timedelta(minutes=30)

        if any([is_instance_newly_created(i) for i in instances]) and item_stats:
            last_segment = item_stats[0]
            if (last_segment['from'] > datetime_to_timestamp(timezone.now() - timedelta(minutes=30)) and
                    'value' not in last_segment):
                last_segment['value'] = 0

        return item_stats
Exemplo n.º 2
0
class ZabbixPublicApiTest(unittest.TestCase):
    def setUp(self):
        self.client = ZabbixDBClient()

    def test_get_item_stats_returns_empty_list_on_db_error(self):
        self.client.zabbix_api_client.get_host_ids = Mock(return_value=[])
        self.client.get_item_time_and_value_list = Mock(
            side_effect=DatabaseError)
        self.assertEqual(self.client.get_item_stats([], 'cpu', 1, 10, 2), [])
Exemplo n.º 3
0
class ZabbixPublicApiTest(unittest.TestCase):
    def setUp(self):
        self.client = ZabbixDBClient()
        self.client.zabbix_api_client.get_host = Mock(
            return_value={'hostid': 1})

    def test_get_item_stats_returns_time_segments(self):
        self.client.get_item_time_and_value_list = Mock(
            return_value=((1415912625L, 1L), (1415912626L, 1L),
                          (1415912627L, 1L), (1415912628L, 1L)))
        start_timestamp = 1415912624L
        end_timestamp = 1415912630L
        segments_count = 3
        instance = object
        item_key = 'cpu'

        segment_list = self.client.get_item_stats([instance], item_key,
                                                  start_timestamp,
                                                  end_timestamp,
                                                  segments_count)

        expected_segment_list = [
            {
                'from': 1415912624L,
                'to': 1415912626L,
                'value': 1
            },
            {
                'from': 1415912626L,
                'to': 1415912628L,
                'value': 1
            },
            {
                'from': 1415912628L,
                'to': 1415912630L,
                'value': 1
            },
        ]
Exemplo n.º 4
0
    def get_stats(self, instance, start, end):
        items = []
        for item in self.validated_data['items']:
            if item == 'memory_util' and instance.type == models.Instance.Services.PAAS:
                items.append('memory_util_agent')
            else:
                items.append(item)
        method = self.validated_data['method']
        host = ZabbixApiClient().get_host_name(instance)

        records = ZabbixDBClient().get_host_max_values(host, items, start, end, method=method)

        results = []
        for timestamp, item, value in records:
            # XXX: Quick and dirty fix: zabbix presents percentage of free space(not utilized) for storage
            if item in ('storage_root_util', 'storage_data_util'):
                results.append({
                    'item': item,
                    'timestamp': timestamp,
                    'value': 100 - value,
                })
            else:
                results.append({
                    'item': item,
                    'timestamp': timestamp,
                    'value': value,
                })
        # XXX: temporary hack: show zero as value if instance was created less then 30 minutes ago and actual value
        # is not available
        items_with_values = {r['item'] for r in results}
        items_without_values = set(items) - items_with_values
        timestamp = datetime_to_timestamp(timezone.now())
        if items_without_values and instance.created > timezone.now() - timedelta(minutes=30):
            for item in items_without_values:
                results.append({'item': item, 'timestamp': timestamp, 'value': 0})
        return results
Exemplo n.º 5
0
 def setUp(self):
     self.client = ZabbixDBClient()
     self.client.zabbix_api_client.get_host = Mock(
         return_value={'hostid': 1})
Exemplo n.º 6
0
 def get_stats(self, instances):
     self.attrs = self.data
     zabbix_db_client = ZabbixDBClient()
     return zabbix_db_client.get_item_stats(
         instances, self.data['item'],
         self.data['start_timestamp'], self.data['end_timestamp'], self.data['segments_count'])
Exemplo n.º 7
0
 def setUp(self):
     self.client = ZabbixDBClient()
Exemplo n.º 8
0
def _get_installation_state(instance):
    db_client = ZabbixDBClient()
    return db_client.get_application_installation_state(instance)