コード例 #1
0
ファイル: cb_common.py プロジェクト: Leonid99/cbtool
def report_app_metrics(metriclist, sla_targets_list, ms_conn = "auto", \
                       os_conn = "auto", reset_syslog = True, force_conversion = None) :

    if ms_conn == "auto" :
        _msci, _my_uuid, _expid, _username = get_ms_conn()
    else :
        _msci = ms_conn[0]
        _my_uuid = ms_conn[1]
        _expid = ms_conn[2]
        _username = ms_conn[3]
        
    if os_conn == "auto" :
        _osci, _my_uuid, _cloud_name = get_os_conn()
    else :
        _osci = os_conn[0]
        _my_uuid = os_conn[1]
        _cloud_name = os_conn[2]

    if reset_syslog :                
        setup_syslog(1)

    try :
        _metrics_dict = {}
        _sla_targets_dict = {}
        _reported_metrics_dict = {}

        _msg = "SLA violation verification"
        cbdebug(_msg)

        for _sla_target in sla_targets_list.split() :
            _sla_target = _sla_target.split(':')
            if len(_sla_target) == 2 :
                if len(_sla_target[1]) :
                    _key = _sla_target[0].replace('sla_runtime_target_','')                
                    _sla_targets_dict[_key] = _sla_target[1]

        _sla_status = "ok"
        for _metric in metriclist.split() :
            _metric = _metric.split(':')
            if len(_metric[1]) :
                _metric[2], _metric[1] = unit_convert(force_conversion, _metric[2], _metric[1])
                _metrics_dict["app_"  + _metric[0]] = {}
                _metrics_dict["app_"  + _metric[0]]["val"] = _metric[1]
                _metrics_dict["app_"  + _metric[0]]["units"] = _metric[2]

                if _metric[0] in _sla_targets_dict :

                    _sla_target, _condition = _sla_targets_dict[_metric[0]].split('-')

                    if len(str(_metric[1])) :

                        _metrics_dict["app_sla_runtime"] = {}
                        _metrics_dict["app_sla_runtime"]["units"] = ' '
                        
                        if _condition == "gt" :
                            if float(_metric[1]) >= float(_sla_target) :
                                True
                            else :
                                _sla_status = "violated"

                        if _condition == "lt" :
                            if float(_metric[1]) <= float(_sla_target) :
                                True
                            else :
                                _sla_status = "violated"

        if "app_sla_runtime" in _metrics_dict :
            _metrics_dict["app_sla_runtime"]["val"] = _sla_status
    
#        tzoffset = datetime.utcfromtimestamp(-1).hour - datetime.fromtimestamp(0).hour + 1
#        _metrics_dict["time"] = int(time()) + (3600 * tzoffset)
        _metrics_dict["time"] = _metrics_dict["time"] = int(mktime(datetime.utcnow().timetuple()))
        _metrics_dict["time_cbtool"] = _osci.get_remote_time()[0]
        _metrics_dict["time_h"] = makeTimestamp() 
        _metrics_dict["time_cbtool_h"] = makeTimestamp(_metrics_dict["time_cbtool"])
        _metrics_dict["expid"] = _expid
        _metrics_dict["uuid"] = _my_uuid
        
        obj_attr_list = False

        _msg = "SLA violation status update"
        cbdebug(_msg)
        
        for _m in [ "sla_runtime", "errors" ] :
            
            if "app_" + _m in _metrics_dict :
                
                obj_attr_list = _osci.get_object(_cloud_name, "VM", False, _my_uuid, False)

                if "sticky_" + _m in obj_attr_list :
                    _previous_m = obj_attr_list["sticky_" + _m]
                    _current_m = _previous_m                    
                else :
                    _previous_m = obj_attr_list[_m]
                    _current_m = _metrics_dict["app_" + _m]["val"]
    
                if is_number(_current_m) and float(_current_m) > 0 :
                    _current_m = "yes"
                
                _xmsg = '.'
                if str(obj_attr_list["sticky_app_status"]).lower() == "true" :                    
                    if _current_m == "violated" or _current_m == "yes" :
                        _xmsg = " (Due to \"application status stickyness)."
                        _osci.update_object_attribute(_cloud_name, \
                                                      "VM", \
                                                      _my_uuid, \
                                                      False, \
                                                      "sticky_" + _m, \
                                                      _current_m)

                if _previous_m == _current_m :
                    _msg = "Previous " + _m + " (\"" + _previous_m
                    _msg += "\") and New (\"" + _current_m + "\")"
                    _msg += " are the same. No updates needed" + _xmsg
                    cbdebug(_msg)
                else :
                    _msg = "Previous " + _m + " status (\"" + _previous_m
                    _msg += "\") and New (\"" + _current_m + "\")"
                    _msg += " are different. Updating attributes and views on the"
                    _msg += " Metric Store"
                    cbdebug(_msg)
                    _osci.update_object_attribute(_cloud_name, \
                                                  "VM", \
                                                  _my_uuid, \
                                                  False, \
                                                  _m, \
                                                  _current_m)

                    obj_attr_list[_m] = _previous_m
                    _osci.remove_from_view(_cloud_name, "VM", obj_attr_list, "BY" + _m.upper())
                    
                    obj_attr_list[_m] = _current_m
                    _osci.add_to_view(_cloud_name, "VM", obj_attr_list, "BY" + _m.upper(), "arrival")

        _msg = "Determine average,min,max"
        cbdebug(_msg)
        
        update_avg_acc_max_min(_metrics_dict, _my_uuid)

        _msg = "Report metrics"
        cbdebug(_msg)
        
        if "app_load_id" in _metrics_dict and _metrics_dict["app_load_id"]["val"] == "1" :
            _new_reported_metrics_dict = {}
            for _key in _metrics_dict.keys() :
                if not _key.count("time") and not _key.count("uuid") and not _key.count("time_h") :
                    _new_reported_metrics_dict[_key] = "1"
            _new_reported_metrics_dict["expid"] = _expid
            _new_reported_metrics_dict["_id"] = b64encode(sha1(_expid).digest())
            _reported_metrics_dict = \
            _msci.find_document("reported_runtime_app_VM_metric_names_" + \
                                _username, {"_id" : _new_reported_metrics_dict["_id"]})
            _reported_metrics_dict.update(_new_reported_metrics_dict)

        _msci.add_document("runtime_app_VM_" + _username, _metrics_dict)
        _msg = "Application Metrics reported successfully. Data package sent was: \"" 
        _msg += str(_metrics_dict) + "\""
        cbdebug(_msg)
    
        _metrics_dict["_id"] = _metrics_dict["uuid"]
        _msci.update_document("latest_runtime_app_VM_" + _username, _metrics_dict)
        _msg = "Latest app performance data updated successfully"
        cbdebug(_msg)

        if len(_reported_metrics_dict) :
            _msci.update_document("reported_runtime_app_VM_metric_names_" + _username, _reported_metrics_dict)
            _msg = "Reported runtime application metric names collection "
            _msg += "updated successfully. Data package sent was: \""
            _msg += str(_reported_metrics_dict) + "\""
            cbdebug(_msg)


        if str(obj_attr_list["notification"]).lower() != "false" :
            if obj_attr_list["notification_channel"].lower() == "auto" :
                _channel = "APPLICATION"
            else :
                _channel = obj_attr_list["notification_channel"]

            _message = "VM object " + _my_uuid + " (" + obj_attr_list["name"] 
            _message += ") submitted a new set of application metrics"

            _osci.publish_message(obj_attr_list["cloud_name"], "VM", _channel, _message, \
                                 1, \
                                 float(obj_attr_list["timeout"]))
        _status = 0
        
    except _msci.MetricStoreMgdConnException, obj :
        _status = obj.status
        _fmsg = str(obj.msg)
コード例 #2
0
def report_app_metrics(metriclist, sla_targets_list, ms_conn = "auto", \
                       os_conn = "auto", reset_syslog = True, force_conversion = None) :

    if ms_conn == "auto":
        _msci, _my_uuid, _expid, _username = get_ms_conn()
    else:
        _msci = ms_conn[0]
        _my_uuid = ms_conn[1]
        _expid = ms_conn[2]
        _username = ms_conn[3]

    if os_conn == "auto":
        _osci, _my_uuid, _cloud_name = get_os_conn()
    else:
        _osci = os_conn[0]
        _my_uuid = os_conn[1]
        _cloud_name = os_conn[2]

    if reset_syslog:
        setup_syslog(1)

    try:
        _metrics_dict = {}
        _sla_targets_dict = {}
        _reported_metrics_dict = {}

        _msg = "SLA violation verification"
        cbdebug(_msg)

        for _sla_target in sla_targets_list.split():
            _sla_target = _sla_target.split(':')
            if len(_sla_target) == 2:
                if len(_sla_target[1]):
                    _key = _sla_target[0].replace('sla_runtime_target_', '')
                    _sla_targets_dict[_key] = _sla_target[1]

        _sla_status = "ok"
        for _metric in metriclist.split():
            _metric = _metric.split(':')
            if len(_metric[1]):
                _metric[2], _metric[1] = unit_convert(force_conversion,
                                                      _metric[2], _metric[1])
                _metrics_dict["app_" + _metric[0]] = {}
                _metrics_dict["app_" + _metric[0]]["val"] = _metric[1]
                _metrics_dict["app_" + _metric[0]]["units"] = _metric[2]

                if _metric[0] in _sla_targets_dict:

                    _sla_target, _condition = _sla_targets_dict[
                        _metric[0]].split('-')

                    if len(str(_metric[1])):

                        _metrics_dict["app_sla_runtime"] = {}
                        _metrics_dict["app_sla_runtime"]["units"] = ' '

                        if _condition == "gt":
                            if float(_metric[1]) >= float(_sla_target):
                                True
                            else:
                                _sla_status = "violated"
                                cbwarn("SLA VIOLATION!!!!!")

                        if _condition == "lt":
                            if float(_metric[1]) <= float(_sla_target):
                                True
                            else:
                                _sla_status = "violated"
                                cbwarn("SLA VIOLATION!!!!!")

        if "app_sla_runtime" in _metrics_dict:
            _metrics_dict["app_sla_runtime"]["val"] = _sla_status

        _metrics_dict["time"] = _metrics_dict["time"] = int(time())
        _metrics_dict["time_cbtool"] = _osci.get_remote_time()[0]
        _metrics_dict["time_h"] = makeTimestamp()
        _metrics_dict["time_cbtool_h"] = makeTimestamp(
            _metrics_dict["time_cbtool"])
        _metrics_dict["expid"] = _expid
        _metrics_dict["uuid"] = _my_uuid

        obj_attr_list = False

        _msg = "SLA violation status update"
        cbdebug(_msg)

        for _m in ["sla_runtime", "errors"]:

            if "app_" + _m in _metrics_dict:

                obj_attr_list = _osci.get_object(_cloud_name, "VM", False,
                                                 _my_uuid, False)

                if "sticky_" + _m in obj_attr_list:
                    _previous_m = obj_attr_list["sticky_" + _m]
                    _current_m = _previous_m
                else:
                    _previous_m = obj_attr_list[_m]
                    _current_m = _metrics_dict["app_" + _m]["val"]

                if is_number(_previous_m):
                    if float(_previous_m) > 0:
                        _previous_m = "yes"
                    else:
                        _previous_m = "no"

                if is_number(_current_m):
                    if float(_current_m) > 0:
                        _current_m = "yes"
                    else:
                        _current_m = "no"

                _username = obj_attr_list["username"]

                _xmsg = '.'
                if str(obj_attr_list["sticky_app_status"]).lower() == "true":
                    if _current_m == "violated" or _current_m == "yes":
                        _xmsg = " (Due to \"application status stickyness)."
                        _osci.update_object_attribute(_cloud_name, \
                                                      "VM", \
                                                      _my_uuid, \
                                                      False, \
                                                      "sticky_" + _m, \
                                                      _current_m)

                if _previous_m == _current_m:
                    _msg = "Previous " + _m + " (\"" + _previous_m
                    _msg += "\") and New (\"" + _current_m + "\")"
                    _msg += " are the same. No updates needed" + _xmsg
                    cbdebug(_msg)
                else:
                    _msg = "Previous " + _m + " status (\"" + _previous_m
                    _msg += "\") and New (\"" + _current_m + "\")"
                    _msg += " are different. Updating attributes and views on the"
                    _msg += " Metric Store"
                    cbdebug(_msg)
                    _osci.update_object_attribute(_cloud_name, \
                                                  "VM", \
                                                  _my_uuid, \
                                                  False, \
                                                  _m, \
                                                  _current_m)

                    obj_attr_list[_m] = _previous_m
                    _osci.remove_from_view(_cloud_name, "VM", obj_attr_list,
                                           "BY" + _m.upper())

                    obj_attr_list[_m] = _current_m
                    _osci.add_to_view(_cloud_name, "VM", obj_attr_list,
                                      "BY" + _m.upper(), "arrival")

        _msg = "Determine average,min,max"
        cbdebug(_msg)

        update_avg_acc_max_min(_metrics_dict, _my_uuid, _username)

        _msg = "Report metrics"
        cbdebug(_msg)

        if "app_load_id" in _metrics_dict and _metrics_dict["app_load_id"][
                "val"] == "1":
            _new_reported_metrics_dict = {}
            for _key in _metrics_dict.keys():
                if not _key.count("time") and not _key.count(
                        "uuid") and not _key.count("time_h"):
                    _new_reported_metrics_dict[_key] = "1"
            _new_reported_metrics_dict["expid"] = _expid
            _new_reported_metrics_dict["_id"] = b64encode(
                sha1(_expid).digest())
            _reported_metrics_dict = \
            _msci.find_document("reported_runtime_app_VM_metric_names_" + \
                                _username, {"_id" : _new_reported_metrics_dict["_id"]})

            if not _reported_metrics_dict:
                _reported_metrics_dict = {}

            _reported_metrics_dict.update(_new_reported_metrics_dict)

        _msci.add_document("runtime_app_VM_" + _username, _metrics_dict)
        _msg = "Application Metrics reported successfully. Data package sent was: \""
        _msg += str(_metrics_dict) + "\""
        cbdebug(_msg)

        _metrics_dict["_id"] = _metrics_dict["uuid"]
        _msci.update_document("latest_runtime_app_VM_" + _username,
                              _metrics_dict)
        _msg = "Latest app performance data updated successfully"
        cbdebug(_msg)

        if len(_reported_metrics_dict):
            _msci.update_document(
                "reported_runtime_app_VM_metric_names_" + _username,
                _reported_metrics_dict)
            _msg = "Reported runtime application metric names collection "
            _msg += "updated successfully. Data package sent was: \""
            _msg += str(_reported_metrics_dict) + "\""
            cbdebug(_msg)

        if str(obj_attr_list["notification"]).lower() != "false":
            if obj_attr_list["notification_channel"].lower() == "auto":
                _channel = "APPLICATION"
            else:
                _channel = obj_attr_list["notification_channel"]

            _message = "VM object " + _my_uuid + " (" + obj_attr_list["name"]
            _message += ") submitted a new set of application metrics"

            _osci.publish_message(obj_attr_list["cloud_name"], "VM", _channel, _message, \
                                 1, \
                                 float(obj_attr_list["timeout"]))

        _status = 0

    except _msci.MetricStoreMgdConnException, obj:
        _status = obj.status
        _fmsg = str(obj.msg)