Add bw, dns, and uptime checks.
[myops.git] / web / collect / client / DNS / lazy.py
1 # $Id: lazy.py,v 1.5.2.4 2011/03/19 22:15:01 customdesigned Exp $
2 #
3 # This file is part of the pydns project.
4 # Homepage: http://pydns.sourceforge.net
5 #
6 # This code is covered by the standard Python License. See LICENSE for details.
7 #
8
9 # routines for lazy people.
10 import Base
11 import string
12
13 from Base import DNSError
14
15 def revlookup(name):
16     "convenience routine for doing a reverse lookup of an address"
17     names = revlookupall(name)
18     if not names: return None
19     return names[0]     # return shortest name
20
21 def revlookupall(name):
22     "convenience routine for doing a reverse lookup of an address"
23     # FIXME: check for IPv6
24     a = string.split(name, '.')
25     a.reverse()
26     b = string.join(a, '.')+'.in-addr.arpa'
27     names = dnslookup(b, qtype = 'ptr')
28     # this will return all records.
29     names.sort(key=str.__len__)
30     return names
31
32 def dnslookup(name,qtype):
33     "convenience routine to return just answer data for any query type"
34     if Base.defaults['server'] == []: Base.DiscoverNameServers()
35     result = Base.DnsRequest(name=name, qtype=qtype).req()
36     if result.header['status'] != 'NOERROR':
37         raise DNSError("DNS query status: %s" % result.header['status'])
38     elif len(result.answers) == 0 and Base.defaults['server_rotate']:
39         # check with next DNS server
40         result = Base.DnsRequest(name=name, qtype=qtype).req()
41     if result.header['status'] != 'NOERROR':
42         raise DNSError("DNS query status: %s" % result.header['status'])
43     return map(lambda x: x['data'],result.answers)
44
45 def mxlookup(name):
46     """
47     convenience routine for doing an MX lookup of a name. returns a
48     sorted list of (preference, mail exchanger) records
49     """
50     l = dnslookup(name, qtype = 'mx')
51     l.sort()
52     return l
53
54 #
55 # $Log: lazy.py,v $
56 # Revision 1.5.2.4  2011/03/19 22:15:01  customdesigned
57 # Added rotation of name servers - SF Patch ID: 2795929
58 #
59 # Revision 1.5.2.3  2011/03/16 20:06:24  customdesigned
60 # Expand convenience methods.
61 #
62 # Revision 1.5.2.2  2011/03/08 21:06:42  customdesigned
63 # Address sourceforge patch requests 2981978, 2795932 to add revlookupall
64 # and raise DNSError instead of IndexError on server fail.
65 #
66 # Revision 1.5.2.1  2007/05/22 20:23:38  customdesigned
67 # Lazy call to DiscoverNameServers
68 #
69 # Revision 1.5  2002/05/06 06:14:38  anthonybaxter
70 # reformat, move import to top of file.
71 #
72 # Revision 1.4  2002/03/19 12:41:33  anthonybaxter
73 # tabnannied and reindented everything. 4 space indent, no tabs.
74 # yay.
75 #
76 # Revision 1.3  2001/08/09 09:08:55  anthonybaxter
77 # added identifying header to top of each file
78 #
79 # Revision 1.2  2001/07/19 06:57:07  anthony
80 # cvs keywords added
81 #
82 #