Package pyhai :: Package plugins :: Module hostname
[hide private]
[frames] | no frames]

Source Code for Module pyhai.plugins.hostname

 1  """ 
 2  @copyright: 2011 Mark LaPerriere 
 3   
 4  @license: 
 5      Licensed under the Apache License, Version 2.0 (the "License"); 
 6      you may not use this file except in compliance with the License. 
 7      You may obtain a copy of the License at 
 8   
 9      U{http://www.apache.org/licenses/LICENSE-2.0} 
10   
11      Unless required by applicable law or agreed to in writing, software 
12      distributed under the License is distributed on an "AS IS" BASIS, 
13      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14      See the License for the specific language governing permissions and 
15      limitations under the License. 
16   
17  @summary: 
18      A cross-platform hostname plugin 
19   
20  @author: Mark LaPerriere 
21  @contact: pyhai@mindmind.com 
22  @organization: Mind Squared Design / www.mindmind.com 
23  @version: 0.1.3 
24  @date: Jan 19, 2012 
25  """ 
26  from pyhai.plugins import AuditorPlugin 
27  import socket 
28  import logging 
29   
30  # set some default logging behavior 
31  _logger = logging.getLogger(__name__) 
32  _logger.addHandler(logging.NullHandler()) 
33   
34 -class HostnamePlugin(AuditorPlugin):
35 - def run(self, *args, **kwargs):
36 fqdn = socket.getfqdn() 37 hostname = socket.gethostname() 38 fqdn_resolved_ip = None 39 hostname_resolved_ip = None 40 try: 41 fqdn_resolved_ip = socket.gethostbyname(fqdn) 42 except socket.gaierror: 43 _logger.exception('Failed to resolve FQDN: %s', fqdn) 44 45 try: 46 hostname_resolved_ip = socket.gethostbyname(hostname) 47 except socket.gaierror: 48 _logger.exception('Failed to resolve hostname: %s', hostname) 49 50 return { 51 'fqdn': fqdn, 52 'fqdn_resolved_ip': fqdn_resolved_ip, 53 'hostname': hostname, 54 'hostname_resolved_ip': hostname_resolved_ip, 55 }
56