Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SoftLayer/CLI/monitor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Monitoring"""
69 changes: 69 additions & 0 deletions SoftLayer/CLI/monitor/status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
usage: sl monitor [<command>] [<args>...] [options]

Manage Monitoring.

The available commands are:
status Show basic monitoring status of servers
"""

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting
from SoftLayer import utils


@click.command()
@click.option('--only_hardware', is_flag=True,
help="Show only physical servers")
@click.option('--only_virtual', is_flag=True,
help="Show only virtual servers")
@environment.pass_env
def cli(env, only_hardware=False, only_virtual=False):
"""shows SERVER_PING status of all servers."""

table = formatting.Table([
'id', 'datacenter', 'FQDN', 'public ip',
'status', 'last checked at'
])

manager = SoftLayer.MonitoringManager(env.client)
if only_virtual:
hardware = []
guest = manager.list_guest_status()
elif only_hardware:
hardware = manager.list_hardware_status()
guest = []
else:
hardware = manager.list_hardware_status()
guest = manager.list_guest_status()

results = hardware + guest
for server in results:
server = utils.NestedDict(server)
res = server['networkMonitors'][0]['lastResult']['responseStatus']
date = server['networkMonitors'][0]['lastResult']['finishTime']
status = 'UNKNOWN'
status_color = None
if res == 0:
status = 'DOWN'
status_color = 'red'
elif res == 1:
status = 'WARNING'
status_color = 'yellow'
elif res == 2:
status = 'OK'
status_color = 'green'

table.add_row([
server['id'],
server['datacenter']['name'] or formatting.blank(),
server['fullyQualifiedDomainName'],
server['primaryIpAddress'] or formatting.blank(),
click.style(status, fg=status_color),
date
])

return table
3 changes: 3 additions & 0 deletions SoftLayer/CLI/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@

('metadata', 'SoftLayer.CLI.metadata:cli'),

('monitor', 'SoftLayer.CLI.monitor'),
('monitor:status', 'SoftLayer.CLI.monitor.status:cli'),

('nas', 'SoftLayer.CLI.nas'),
('nas:list', 'SoftLayer.CLI.nas.list:cli'),

Expand Down
3 changes: 2 additions & 1 deletion SoftLayer/managers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from SoftLayer.managers.load_balancer import LoadBalancerManager # NOQA
from SoftLayer.managers.messaging import MessagingManager # NOQA
from SoftLayer.managers.metadata import MetadataManager # NOQA
from SoftLayer.managers.monitor import MonitoringManager # NOQA
from SoftLayer.managers.network import NetworkManager # NOQA
from SoftLayer.managers.ordering import OrderingManager # NOQA
from SoftLayer.managers.sshkey import SshKeyManager # NOQA
Expand All @@ -30,4 +31,4 @@
'ImageManager', 'MessagingManager', 'MetadataManager', 'CDNManager',
'NetworkManager', 'SshKeyManager', 'SSLManager', 'TicketManager',
'VSManager', 'ISCSIManager', 'LoadBalancerManager',
'OrderingManager']
'OrderingManager', 'MonitoringManager']
85 changes: 85 additions & 0 deletions SoftLayer/managers/monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
SoftLayer.monitor
~~~~~~~~~~~~~~~~~~
Monitoring Manager/helpers

:license: MIT, see LICENSE for more details.
"""


from SoftLayer import utils


class MonitoringManager(utils.IdentifierMixin, object):
"""Manages monitoring for Hardware and Virtual Servers

:param SoftLayer.API.CLient client: an API client instance
:param string server_tpe: should be either
'Hardware_Server' or 'Virtual_Guest'
"""

def __init__(self, client, server_type='Hardware_Server'):
self.client = client
self.account = self.client['Account']
self.server = self.client[server_type]

def get_status(self, server_id):
"""get the monitoring status of a server

:param int server_id: the id of the server

Definition of some of the monitoring status codes
0 = Down
1 = Warning
2 = OK
>2 = something strange happened
"""

mask = "lastResult, subnet[virtualGuests,hardware]"
agent = self.server.getNetworkMonitors(id=server_id, mask=mask)
return agent

def list_hardware_status(self, **kwargs):
"""List all hardware with their monitoring status

:param dict \\*\\*kwargs: response-level options (mask, limit, filter)
:returns: Retrns a list of dictionaries with server and monitoring
information.
"""

return self._get_network_monitors('getHardware', **kwargs)

def list_guest_status(self, **kwargs):
"""List all virtual guests with their monitoring status

:param dict \\*\\*kwargs: response-level options (mask, limit, filter)
:returns: Retrns a list of dictionaries with server and monitoring
information.
"""

return self._get_network_monitors('getVirtualGuests', **kwargs)

def _get_network_monitors(self, call, **kwargs):
"""Does all the actual work of getting the servers monitoring status

:param string call: method from the account class to call
'getHardware' or 'getVirtualGuests'
:param dict \\*\\*kwargs: response-level options (mask, limit, filter)
:returns: Retrns a list of dictionaries with server and monitoring
information.
"""

if 'mask' not in kwargs:
vs_items = [
'id',
'fullyQualifiedDomainName',
'primaryBackendIpAddress',
'primaryIpAddress',
'datacenter',
'networkMonitors[lastResult]'
]

kwargs['mask'] = ('[mask[%s]]'
% (','.join(vs_items)))
function = getattr(self.account, call)
return function(**kwargs)
Empty file.
32 changes: 28 additions & 4 deletions SoftLayer/testing/fixtures/SoftLayer_Account.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
'globalIdentifier': '1a2b3c-1701',
'primaryBackendIpAddress': '10.45.19.37',
'hourlyBillingFlag': False,

'billingItem': {
'id': 6327,
'recurringFee': 1.54,
Expand All @@ -43,6 +42,11 @@
}
}
},
'networkMonitors': [{
'guestId': 100,
'hardwareId': '',
'lastResult': {'responseStatus': 2}
}],
}, {
'id': 104,
'hostname': 'vs-test2',
Expand All @@ -69,6 +73,11 @@
}
}
},
'networkMonitors': [{
'guestId': 104,
'hardwareId': '',
'lastResult': {'responseStatus': 0}
}],
}]

getMonthlyVirtualGuests = [vs for vs in getVirtualGuests
Expand Down Expand Up @@ -135,7 +144,12 @@
'friendlyName': 'Friendly Transaction Name',
'id': 6660
}
}
},
'networkMonitors': [{
'guestId': '',
'hardwareId': 1000,
'lastResult': {'responseStatus': 2}
}],
}, {
'id': 1001,
'datacenter': {'name': 'TEST00',
Expand Down Expand Up @@ -178,7 +192,12 @@
'vlanNumber': 3672,
'id': 19082
},
]
],
'networkMonitors': [{
'guestId': '',
'hardwareId': 1001,
'lastResult': {'responseStatus': 0}
}],
}, {
'id': 1002,
'datacenter': {'name': 'TEST00',
Expand Down Expand Up @@ -221,7 +240,12 @@
'vlanNumber': 3672,
'id': 19082
},
]
],
'networkMonitors': [{
'guestId': '',
'hardwareId': 1002,
'lastResult': {'responseStatus': 2}
}],
}]
getDomains = [{'name': 'example.com',
'id': 12345,
Expand Down
27 changes: 27 additions & 0 deletions SoftLayer/tests/managers/monitor_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
SoftLayer.tests.managers.monitor_tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:license: MIT, see LICENSE for more details.
"""
import SoftLayer
from SoftLayer import testing
from SoftLayer.testing import fixtures


class MonitorTests(testing.TestCase):

def set_up(self):
self.monitor_manager = SoftLayer.MonitoringManager(self.client)

def test_list_hardware_status(self):
result = self.monitor_manager.list_hardware_status()

self.assertEqual(result, fixtures.SoftLayer_Account.getHardware)
self.assert_called_with('SoftLayer_Account', 'getHardware')

def test_list_guest_status(self):
result = self.monitor_manager.list_guest_status()

self.assertEqual(result, fixtures.SoftLayer_Account.getVirtualGuests)
self.assert_called_with('SoftLayer_Account', 'getVirtualGuests')