1313
1414
1515class Plugin :
16+
1617 @classmethod
1718 def create_record (cls , metric , value , lain_info , metric_type = "GAUGE" ):
1819 interval = CONFIGS ['interval' ]
@@ -26,15 +27,21 @@ def create_record(cls, metric, value, lain_info, metric_type="GAUGE"):
2627
2728
2829class CpuStats (Plugin ):
30+
2931 @classmethod
3032 def get (cls , stats , lain_info ):
31- # docker cpu stats is nanoseconds, plus 100 for percent, *100/1e9 = /1e7
32- cls .create_record ("cpu.total" , int (int (stats ["cpu_stats" ]["cpu_usage" ]["total_usage" ])/ 1e7 ), lain_info )
33- cls .create_record ("cpu.user" , int (int (stats ["cpu_stats" ]["cpu_usage" ]["usage_in_usermode" ])/ 1e7 ), lain_info )
34- cls .create_record ("cpu.kernel" , int (int (stats ["cpu_stats" ]["cpu_usage" ]["usage_in_kernelmode" ])/ 1e7 ), lain_info )
33+ # docker cpu stats is nanoseconds, plus 100 for percent, *100/1e9 =
34+ # /1e7
35+ cls .create_record ("cpu.total" , int (
36+ int (stats ["cpu_stats" ]["cpu_usage" ]["total_usage" ]) / 1e7 ), lain_info )
37+ cls .create_record ("cpu.user" , int (
38+ int (stats ["cpu_stats" ]["cpu_usage" ]["usage_in_usermode" ]) / 1e7 ), lain_info )
39+ cls .create_record ("cpu.kernel" , int (
40+ int (stats ["cpu_stats" ]["cpu_usage" ]["usage_in_kernelmode" ]) / 1e7 ), lain_info )
3541
3642
3743class MemoryStats (Plugin ):
44+
3845 @classmethod
3946 def get (cls , stats , lain_info ):
4047 usage = stats ["memory_stats" ]["usage" ]
@@ -62,12 +69,25 @@ def get(cls, stats, lain_info):
6269 "sectors_recursive": []
6370}
6471"""
72+
73+ # blkio-io_service_bytes_recursive-253-0-READ
74+ BLKIO_KEY_FORMAT = 'blkio.%s-%s-%s-%s'
75+
76+
6577class BlkioStats (Plugin ):
78+
6679 @classmethod
6780 def get (cls , stats , lain_info ):
6881 for stat , value in stats ["blkio_stats" ].iteritems ():
82+ blk_stats = {}
6983 for item in value :
70- cls .create_record ("blkio.%s-%s" % (stat , item ['op' ]), item ['value' ], lain_info , "COUNTER" )
84+ key = BLKIO_KEY_FORMAT % (
85+ stat , item ['major' ], item ['minor' ], item ['op' ])
86+ blk_stats [key ] = item ['value' ]
87+
88+ for key , value in blk_stats .iteritems ():
89+ cls .create_record (
90+ key , value , lain_info , "COUNTER" )
7191
7292
7393"""
@@ -82,14 +102,18 @@ def get(cls, stats, lain_info):
82102 "tx_packets": 0
83103},
84104"""
105+
106+
85107class NetworkStats (Plugin ):
108+
86109 @classmethod
87110 def get (cls , stats , lain_info ):
88111 if "networks" not in stats :
89112 return
90113 for interface in stats ["networks" ]:
91114 for stat in stats ["networks" ][interface ]:
92- cls .create_record ("net.%s-%s" % (interface , stat ), stats ["networks" ][interface ][stat ], lain_info , "COUNTER" )
115+ cls .create_record ("net.%s-%s" % (interface , stat ),
116+ stats ["networks" ][interface ][stat ], lain_info , "COUNTER" )
93117
94118
95119class Docker :
@@ -123,7 +147,6 @@ def get_all_running_containers(cls):
123147 return result
124148
125149
126-
127150class Lainlet (object ):
128151
129152 def __init__ (self , url , hostname ):
@@ -149,7 +172,8 @@ def get_containers(self):
149172 return info
150173
151174 def get_depends (self ):
152- # portal: DOMAIN.app.[SERVICE|RESOURCE].portal.PORTALNAME.APPNAME.NODENAME.(instance.NO.)
175+ # portal:
176+ # DOMAIN.app.[SERVICE|RESOURCE].portal.PORTALNAME.APPNAME.NODENAME.(instance.NO.)
153177 url = "%s/v2/depends" % (self .url )
154178 r = urllib .urlopen (url )
155179 depends = json .loads (r .read ())
@@ -158,12 +182,14 @@ def get_depends(self):
158182 for host , hval in val .iteritems ():
159183 for app , aval in hval .iteritems ():
160184 service_name , _ , _ = key .rsplit ('.' , 2 )
161- service_name = service_name .replace ('.' , '_' ) # for resource
185+ service_name = service_name .replace (
186+ '.' , '_' ) # for resource
162187 name = "%s-%s-%s" % (key , host , app )
163188 info [name ] = {}
164189 info [name ]['app_name' ] = app
165190 info [name ]['node_name' ] = host
166- info [name ]['portal_name' ] = json .loads (aval ['Annotation' ])['service_name' ]
191+ info [name ]['portal_name' ] = json .loads (
192+ aval ['Annotation' ])['service_name' ]
167193 info [name ]['service_name' ] = service_name
168194 info [name ]['proc_name' ] = None
169195 info [name ]['proc_type' ] = None
@@ -207,9 +233,9 @@ def get_info(cls, containers, depends, container_id, container_name):
207233if __name__ == "__main__" :
208234 parser = argparse .ArgumentParser ()
209235 parser .add_argument ("--lainlet-endpoint" , help = "lainlet endpoint" ,
210- default = "http://lainlet.lain:9001" , type = str )
236+ default = "http://lainlet.lain:9001" , type = str )
211237 parser .add_argument ("--domain" , help = "lain domain" ,
212- default = "lain.local" , type = str )
238+ default = "lain.local" , type = str )
213239 args = parser .parse_args ()
214240 CONFIGS ['domain' ] = args .domain .replace ('.' , '_' )
215241 lainlet = Lainlet (args .lainlet_endpoint , CONFIGS ['hostname' ])
@@ -225,7 +251,8 @@ def get_info(cls, containers, depends, container_id, container_name):
225251 container_name = container ["Names" ][0 ].strip ('/' )
226252 container_id = container ["Id" ]
227253 stats = Docker .get_stats (container_id )
228- lain_info = lainlet .get_info (containers , depends , container_id , container_name )
254+ lain_info = lainlet .get_info (
255+ containers , depends , container_id , container_name )
229256
230257 for klass in CLASSES :
231258 klass .get (stats , lain_info )
0 commit comments