2121import os
2222import socket
2323import threading
24- import msg
2524import ssl
2625from subprocess import Popen , PIPE
2726from sys import exc_info
2827from glob import glob
29-
28+ import re
3029try :
3130 from urlparse import urlparse
3231except ImportError :
3332 from urllib .parse import urlparse
34-
3533try :
3634 import urllib .request as urllib2
3735except ImportError :
3836 import urllib2
39-
4037try :
4138 import MySQLdb
4239 PYMYSQL = True
4643 PYMYSQL = True
4744 except ImportError :
4845 PYMYSQL = False
46+ import msg
4947
5048try :
5149 PATH = os .getenv ('PATH' ).split (':' )
@@ -175,14 +173,15 @@ def run(self):
175173 # it is important to do this in a loop
176174 # sleep() is interruptable
177175 while now < next :
178- self .debug ("sleeping for" , str (next - now ), "secs to reach frequency of" , str (step ), "secs, now:" , str (now ), " next:" , str (next ), " penalty:" , str (penalty ))
176+ self .debug ("sleeping for" , str (next - now ), "secs to reach frequency of" ,
177+ str (step ), "secs, now:" , str (now ), " next:" , str (next ), " penalty:" , str (penalty ))
179178 time .sleep (next - now )
180179 now = float (time .time ())
181180
182181 # do the job
183182 try :
184183 status = self ._run_once ()
185- except Exception as e :
184+ except Exception :
186185 status = False
187186
188187 if status :
@@ -202,10 +201,12 @@ def run(self):
202201 penalty = 600
203202
204203 self .retries_left = self .retries
205- self .alert ("failed to collect data for " + str (self .retries ) + " times - increasing penalty to " + str (penalty ) + " sec and trying again" )
204+ self .alert ("failed to collect data for " + str (self .retries ) +
205+ " times - increasing penalty to " + str (penalty ) + " sec and trying again" )
206206
207207 else :
208- self .error ("failed to collect data - " + str (self .retries_left ) + " retries left - penalty: " + str (penalty ) + " sec" )
208+ self .error ("failed to collect data - " + str (self .retries_left )
209+ + " retries left - penalty: " + str (penalty ) + " sec" )
209210
210211 # --- CHART ---
211212
@@ -460,11 +461,42 @@ def find_binary(binary):
460461 return next (('/' .join ([p , binary ]) for p in PATH
461462 if os .path .isfile ('/' .join ([p , binary ]))
462463 and os .access ('/' .join ([p , binary ]), os .X_OK )))
463- else :
464- return None
464+ return None
465465 except StopIteration :
466466 return None
467467
468+ def _add_new_dimension (self , dimension_id , chart_name , dimension = None , algorithm = 'incremental' ,
469+ multiplier = 1 , divisor = 1 , priority = 65000 ):
470+ """
471+ :param dimension_id:
472+ :param chart_name:
473+ :param dimension:
474+ :param algorithm:
475+ :param multiplier:
476+ :param divisor:
477+ :param priority:
478+ :return:
479+ """
480+ if not all ([dimension_id not in self ._dimensions ,
481+ chart_name in self .order ,
482+ chart_name in self .definitions ]):
483+ return
484+ self ._dimensions .append (dimension_id )
485+ dimension_list = list (map (str , [dimension_id ,
486+ dimension if dimension else dimension_id ,
487+ algorithm ,
488+ multiplier ,
489+ divisor ]))
490+ self .definitions [chart_name ]['lines' ].append (dimension_list )
491+ add_to_name = self .override_name or self .name
492+ job_name = ('_' .join ([self .__module__ , re .sub ('\s+' , '_' , add_to_name )])
493+ if add_to_name != 'None' else self .__module__ )
494+ chart = 'CHART {0}.{1} ' .format (job_name , chart_name )
495+ options = '"" "{0}" {1} "{2}" {3} {4} ' .format (* self .definitions [chart_name ]['options' ][1 :6 ])
496+ other = '{0} {1}\n ' .format (priority , self .update_every )
497+ new_dimension = "DIMENSION {0}\n " .format (' ' .join (dimension_list ))
498+ print (chart + options + other + new_dimension )
499+
468500
469501class UrlService (SimpleService ):
470502 def __init__ (self , configuration = None , name = None ):
0 commit comments