1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | # This is the MIT License |
---|
4 | # http://www.opensource.org/licenses/mit-license.php |
---|
5 | # |
---|
6 | # Copyright (c) 2007 Nick Galbreath |
---|
7 | # |
---|
8 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
9 | # of this software and associated documentation files (the "Software"), to deal |
---|
10 | # in the Software without restriction, including without limitation the rights |
---|
11 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
12 | # copies of the Software, and to permit persons to whom the Software is |
---|
13 | # furnished to do so, subject to the following conditions: |
---|
14 | # |
---|
15 | # The above copyright notice and this permission notice shall be included in |
---|
16 | # all copies or substantial portions of the Software. |
---|
17 | # |
---|
18 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
19 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
20 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
21 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
22 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
23 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
24 | # THE SOFTWARE. |
---|
25 | # |
---|
26 | |
---|
27 | # |
---|
28 | # Version 1.0 - 21-April2-2007 |
---|
29 | # |
---|
30 | |
---|
31 | # |
---|
32 | # Modified by: Ramon Bastiaans |
---|
33 | # For the Job Monarch Project, see: https://subtrac.sara.nl/oss/jobmonarch/ |
---|
34 | # |
---|
35 | # added: DEFAULT_TYPE for Gmetric's |
---|
36 | # added: checkHostProtocol to determine if target is multicast or not |
---|
37 | # changed: allow default for Gmetric constructor |
---|
38 | # changed: allow defaults for all send() values except dmax |
---|
39 | # |
---|
40 | |
---|
41 | import xdrlib, socket |
---|
42 | |
---|
43 | GMETRIC_DEFAULT_TYPE = 'string' |
---|
44 | GMETRIC_DEFAULT_HOST = '127.0.0.1' |
---|
45 | GMETRIC_DEFAULT_PORT = '8649' |
---|
46 | |
---|
47 | class Gmetric: |
---|
48 | |
---|
49 | global GMETRIC_DEFAULT_HOST, GMETRIC_DEFAULT_PORT |
---|
50 | |
---|
51 | slope = { 'zero' : 0, 'positive' : 1, 'negative' : 2, 'both' : 3, 'unspecified' : 4 } |
---|
52 | type = ( '', 'string', 'uint16', 'int16', 'uint32', 'int32', 'float', 'double', 'timestamp' ) |
---|
53 | protocol = ( 'udp', 'multicast' ) |
---|
54 | |
---|
55 | def __init__( self, host=GMETRIC_DEFAULT_HOST, port=GMETRIC_DEFAULT_PORT ): |
---|
56 | |
---|
57 | global GMETRIC_DEFAULT_TYPE |
---|
58 | |
---|
59 | self.prot = self.checkHostProtocol( host ) |
---|
60 | self.msg = xdrlib.Packer() |
---|
61 | self.socket = socket.socket( socket.AF_INET, socket.SOCK_DGRAM ) |
---|
62 | |
---|
63 | if self.prot not in self.protocol: |
---|
64 | |
---|
65 | raise ValueError( "Protocol must be one of: " + str( self.protocol ) ) |
---|
66 | |
---|
67 | if self.prot == 'multicast': |
---|
68 | |
---|
69 | self.socket.setsockopt( socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 20 ) |
---|
70 | |
---|
71 | self.hostport = ( host, int( port ) ) |
---|
72 | self.type = GMETRIC_DEFAULT_TYPE |
---|
73 | self.unitstr = '' |
---|
74 | self.slopestr = 'both' |
---|
75 | self.tmax = 60 |
---|
76 | |
---|
77 | def checkHostProtocol( self, ip ): |
---|
78 | |
---|
79 | MULTICAST_ADDRESS_MIN = ( "224", "0", "0", "0" ) |
---|
80 | MULTICAST_ADDRESS_MAX = ( "239", "255", "255", "255" ) |
---|
81 | |
---|
82 | ip_fields = ip.split( '.' ) |
---|
83 | |
---|
84 | if ip_fields >= MULTICAST_ADDRESS_MIN and ip_fields <= MULTICAST_ADDRESS_MAX: |
---|
85 | |
---|
86 | return 'multicast' |
---|
87 | else: |
---|
88 | return 'udp' |
---|
89 | |
---|
90 | def send( self, name, value, dmax ): |
---|
91 | |
---|
92 | msg = self.makexdr( name, value, self.type, self.unitstr, self.slopestr, self.tmax, dmax ) |
---|
93 | |
---|
94 | return self.socket.sendto( msg, self.hostport ) |
---|
95 | |
---|
96 | def makexdr( self, name, value, typestr, unitstr, slopestr, tmax, dmax ): |
---|
97 | |
---|
98 | if slopestr not in self.slope: |
---|
99 | |
---|
100 | raise ValueError( "Slope must be one of: " + str( self.slope.keys() ) ) |
---|
101 | |
---|
102 | if typestr not in self.type: |
---|
103 | |
---|
104 | raise ValueError( "Type must be one of: " + str( self.type ) ) |
---|
105 | |
---|
106 | if len( name ) == 0: |
---|
107 | |
---|
108 | raise ValueError( "Name must be non-empty" ) |
---|
109 | |
---|
110 | self.msg.reset() |
---|
111 | self.msg.pack_int( 0 ) |
---|
112 | self.msg.pack_string( typestr ) |
---|
113 | self.msg.pack_string( name ) |
---|
114 | self.msg.pack_string( str( value ) ) |
---|
115 | self.msg.pack_string( unitstr ) |
---|
116 | self.msg.pack_int( self.slope[ slopestr ] ) |
---|
117 | self.msg.pack_uint( int( tmax ) ) |
---|
118 | self.msg.pack_uint( int( dmax ) ) |
---|
119 | |
---|
120 | return self.msg.get_buffer() |
---|