예제 #1
0
class PerfManager:

    def __init__(self):
        self.performance_desc_string = ""
        self._info_manager = InfoManager()
        self._info_manager.tiny_update()

        self._last_filled_perf = None

    def clear_perfdata(self):
        global perfdata_list
        perfdata_list = []

    def add_perfdata(self, name, value=None, warning_threshold=None, critical_threshold=None, state=0):

        global perfdata_list
        global perf_counter

        perf_data = _PerfData()
        perf_data.name = str(name)

        try:
            perf_data.value = float(value)
            if perf_data.counter == -1:
                perf_data.counter = perf_counter
                perf_counter = perf_counter + 1
        except:
            perf_data.value = ""

        try:
            perf_data.warning_threshold = float(warning_threshold)
        except:
            perf_data.warning_threshold = ""

        try:
            perf_data.critical_threshold = float(critical_threshold)
        except:
             perf_data.critical_threshold = ""

        try:
            perf_data.state = int(state)
        except:
             perf_data.state = 3

        cnt = 0

        for perf_data_in_list in perfdata_list:
            if perf_data_in_list.name == perf_data.name:

                perfdata_list[cnt] = perf_data
                return

            cnt = cnt + 1

        perfdata_list.append(perf_data)

    def rename_perfdata(self, old_name, new_name, warning_threshold="", critical_threshold=""):

        global perfdata_list

        perfdata_list_copy = copy.deepcopy(perfdata_list)

        cnt = 0
        for perf_data_in_list in perfdata_list:
            if perf_data_in_list.name == str(new_name):
                del perfdata_list_copy[cnt]
                cnt = cnt - 1
            elif perf_data_in_list.name == str(old_name):

                perfdata_list_copy[cnt].name = str(new_name)

                try:
                    new_warning_threshold = float(warning_threshold)
                    perfdata_list_copy[cnt].warning_threshold = new_warning_threshold
                except:
                    pass

                try:
                    new_critical_threshold = float(critical_threshold)
                    perfdata_list_copy[cnt].critical_threshold = new_critical_threshold
                except:
                    pass

            cnt = cnt + 1

        perfdata_list = copy.deepcopy(perfdata_list_copy)

    def get_perfdata(self, name, delete_perfdata=False):

        global perfdata_list
        ret_val = None
        perfdata_list_copy = copy.deepcopy(perfdata_list)

        cnt = 0
        for perf_data_in_list in perfdata_list:
            if perf_data_in_list.name == name:

                if perf_data_in_list.value == "" or perf_data_in_list.value is None:
                    raise Exception('Perf data value is Null')

                if delete_perfdata is True:
                    del perfdata_list_copy[cnt]

                ret_val = perf_data_in_list.value
            cnt = cnt + 1

        perfdata_list = copy.deepcopy(perfdata_list_copy)
        perfdata_list_copy = []
        return ret_val

    def get_all_perfdata(self):
        global perfdata_list
        return copy.deepcopy(perfdata_list)

    def delete_perfdata(self, name):

        global perfdata_list

        perfdata_list_copy = copy.deepcopy(perfdata_list)

        cnt = 0
        for perf_data_in_list in perfdata_list:
            if perf_data_in_list.name == name:

                del perfdata_list_copy[cnt]

            cnt = cnt + 1

        perfdata_list = copy.deepcopy(perfdata_list_copy)
        perfdata_list_copy = []

    def sum_perfdata(self, *names, **kwargs):

        global perfdata_list

        sum = None

        value_to_sum = []
        index_to_delete = []
        perfdata_list_copy = []

        delete_perf = False
        perf_name = ""
        warning_threshold  = None
        critical_threshold = None

        try:
            delete_perf = kwargs['delete_perfdata']
        except:
            pass

        try:
            perf_name = kwargs['name']
        except:
            pass

        try:
            warning_threshold = float(kwargs['warning_threshold'])
        except:
            pass

        try:
            critical_threshold = float(kwargs['critical_threshold'])
        except:
            pass

        cnt = 0
        for perf_data_in_list in perfdata_list:
            for name in names:
                if perf_data_in_list.name == name and perf_data_in_list.value != ""\
                        and perf_data_in_list.value is not None:
                    value_to_sum.append(perf_data_in_list.value)
                    sum = 0 #init sum

                    if delete_perf is True:
                        index_to_delete.append(cnt)

                elif perf_data_in_list.name == name and (perf_data_in_list.value == ""\
                        or perf_data_in_list.value is None):
                    raise Exception('You cannot sum empty value(s)')

                """
                elif (delete_perf is True or perf_name != "") and perf_data_in_list.name == name:
                    index_to_delete.append(cnt)
                """

            cnt = cnt + 1

        cnt = 0
        for perf in perfdata_list:

            if cnt not in index_to_delete:
                perfdata_list_copy.append(perf)

            cnt = cnt + 1

        perfdata_list = copy.deepcopy(perfdata_list_copy)
        perfdata_list_copy = []

        for perf in value_to_sum:

            sum = sum + perf

        if perf_name != "":
            self.add_perfdata(perf_name, sum, warning_threshold, critical_threshold)

        return sum

    def order_perfdata(self):
        global perfdata_list
        perfdata_ok_list = []
        perfdata_notok_list = []

        for perf_data_in_list in perfdata_list:

            if perf_data_in_list.counter != -1:
                perfdata_ok_list.append(copy.deepcopy(perf_data_in_list))
            else:
                perfdata_notok_list.append(copy.deepcopy(perf_data_in_list))

        perfdata_ok_list.sort(key=lambda x: x.counter, reverse=False)

        if len(perfdata_ok_list) > 0:
            self._last_filled_perf = perfdata_ok_list[-1].name

        perfdata_list = []
        perfdata_list = perfdata_ok_list + perfdata_notok_list

    def get_perfdata_string(self):

        global perfdata_list

        ret_string = ""
        cnt = 0
        for perfdata in perfdata_list:

            name = perfdata.name
            value = perfdata.value
            warning = perfdata.warning_threshold
            critical = perfdata.critical_threshold

            if cnt == 0:
                ret_string = ret_string + name + "=" + str(value) + "s;" + str(warning) + ";" + str(critical) + ";;"
            else:
                ret_string = ret_string + " " + name + "=" + str(value) + "s;" + str(warning) + ";" + str(critical) + ";;"

            cnt = cnt + 1

        return ret_string

    def get_output(self, message=None, print_output=True):

        prefix_robot_framework = ""

        global perfdata_list
        global timedout_finders

        self.order_perfdata()

        exitcode = self.get_exitcode()
        performanceData = self.get_perfdata_string()

        if performanceData is not "":
            performanceData = "|" + performanceData
        else:
            performanceData = ""

        if message is not None:
            self.performance_desc_string = self.performance_desc_string + message + performanceData + os.linesep
        elif exitcode == 2:
            self.performance_desc_string = self.performance_desc_string +\
                                           "CRITICAL: one or more steps are in critical state" +\
                                           performanceData + os.linesep
            prefix_robot_framework = "*WARN*"
        elif exitcode == 1:
            self.performance_desc_string = self.performance_desc_string +\
                                           "WARNING: one or more steps are in warning state" +\
                                           performanceData + os.linesep
            prefix_robot_framework = "*WARN*"
        elif exitcode == 3:
            if self._last_filled_perf is not None:
                self.performance_desc_string = self.performance_desc_string +\
                                               "UNKNOWN: some error occurred, last filled perf data is " +\
                                               self._last_filled_perf + " " + performanceData + os.linesep
            else:
                self.performance_desc_string = self.performance_desc_string +\
                                               "UNKNOWN: some error occurred, no perf data was filled" +\
                                               performanceData + os.linesep
            prefix_robot_framework = "*WARN*"
        elif len(timedout_finders) > 0:
            self.performance_desc_string = self.performance_desc_string +\
                                           "CRITICAL: one or more steps are in timeout state" +\
                                           performanceData + os.linesep
            prefix_robot_framework = "*WARN*"
        else:
            self.performance_desc_string = self.performance_desc_string +\
                                           "OK: all steps are ok" +\
                                           performanceData + os.linesep

        for perfdata in perfdata_list:

            name = perfdata.name
            value = perfdata.value
            warning = perfdata.warning_threshold
            critical = perfdata.critical_threshold
            #state = perfdata.state

            #only for Alyvix
            state = 3
            if value != "" and critical != "" and value >= critical:
                state = 2
            elif value != "" and warning != "" and value >= warning:
                state = 1
            elif value != "":
                state = 0
            elif value == "" and warning == "" and critical == "" and state == 0:
                state = 3

            if state == 0:
                self.performance_desc_string = self.performance_desc_string +\
                                               "OK: " + name + " time is " + str(value) + " sec." + os.linesep
            elif state == 1:
                self.performance_desc_string = self.performance_desc_string +\
                                               "WARNING: " + name + " time is " + str(value) + " sec." + os.linesep
            elif state == 2:
                self.performance_desc_string = self.performance_desc_string +\
                                               "CRITICAL: " + name + " time is " + str(value) + " sec." +\
                                               os.linesep
            else:
                if value != "":
                    self.performance_desc_string = self.performance_desc_string +\
                                                   "UNKNOWN: " + name + " time is " + str(value) + " sec." + os.linesep
                elif value == "":
                    self.performance_desc_string = self.performance_desc_string +\
                                                   "UNKNOWN: " + name + " time is null." + os.linesep


        os.environ["alyvix_exitcode"] = str(exitcode)
        os.environ["alyvix_std_output"] = self.performance_desc_string

        if self._info_manager.get_info("ROBOT CONTEXT") is True:

            suite_source = self._info_manager.get_info("SUITE SOURCE")

            file_name = suite_source.split(os.sep)[-1].split(".")[0]

            result_dir = tempfile.gettempdir() + os.sep + "alyvix_pybot" + os.sep + file_name + os.sep + "result"
        else:
            result_dir = "."

        if not os.path.exists(result_dir):
            os.makedirs(result_dir)

        text_file = open(result_dir + os.sep + "message.txt", "w")
        text_file.write(self.performance_desc_string)
        text_file.close()

        text_file = open(result_dir + os.sep + "exitcode.txt", "w")
        text_file.write(str(exitcode))
        text_file.close()

        if print_output is True:
            print prefix_robot_framework + self.performance_desc_string
        return exitcode

    def get_exitcode(self):

        global perfdata_list
        exitcode = 0

        for perfdata in perfdata_list:

            name = perfdata.name
            value = perfdata.value
            warning = perfdata.warning_threshold
            critical = perfdata.critical_threshold
            state = perfdata.state

            #only for Alyvix
            if value != "" and critical != "" and value >= critical:
                state = 2
            elif value != "" and warning != "" and value >= warning:
                state = 1
            elif value == "" and warning == "" and critical == "" and state == 0:
                state = 3

            if state > exitcode:
                exitcode = state

            if exitcode == 2:
                break

        return exitcode
예제 #2
0
class LogManager:

    #__image = None
    #__main_object_points = None
    #__sub_objects_points = []

    def __init__(self):

        self._config_reader = ConfigReader()

        self.__enable_log = self._config_reader.get_log_enable_value()

        self.__set_log_path()

        self._robot_manager = RobotManager()

        self._info_manager = InfoManager()
        self._info_manager.tiny_update()

        self._robot_context = self._info_manager.get_info("ROBOT CONTEXT")

    def set_object_name(self, name):
        """
        set the object name.

        :type name: string
        :param name: the object name
        """

        current_time = datetime.now().strftime("%H_%M_%S.%f")

        current_time = current_time[:-4]

        self.__name = name
        self.__name_with_time = current_time + "_" + name

    def __set_log_path(self, path=None):

        global log_path

        #print "log_path:", root_log_path

        test_case_name = os.getenv("alyvix_test_case_name", "generic")
        #print test_case_name

        #set testcase root log folder
        if log_path is None and self.__enable_log is True:

            day_time = time.strftime("%d-%m-%y")
            hh_mm_ss = time.strftime("%H_%M_%S")

            root_log_path = self._config_reader.get_log_folder()

            #print root_log_path
            path = root_log_path + os.sep + test_case_name + os.sep + day_time + os.sep + hh_mm_ss

            try:
                if not os.path.exists(path):
                    os.makedirs(path)
            except:
                pass

            log_path = path
            #print path

            self.__delete_old_folders(root_log_path + os.sep + test_case_name)

    def __delete_old_folders(self, root_path):

        daily_folders = os.listdir(root_path)

        for daily_folder in daily_folders:

        
            try:
                if not "-" in daily_folder:
                    continue
                daily_folder_datetime = datetime.strptime(daily_folder, "%d-%m-%y")
                current_datetime = datetime.now()

                current_full_path = root_path + os.sep + daily_folder

                log_max_days = os.getenv("alyvix_log_max_days", "7")
                log_max_days_int = int(log_max_days)

                #if log folder are too old, then delete it
                if daily_folder_datetime <= current_datetime + timedelta(days=(log_max_days_int * -1)):
                    shutil.rmtree(current_full_path)
                #otherwise, check if we have to delete hours folder
                elif daily_folder == time.strftime("%d-%m-%y"):
                    minutes_folders = os.listdir(current_full_path)

                    for minutes_folder in minutes_folders:
                        try:

                            if not "-" in minutes_folder:
                                continue

                            #create fake datetime, that because we need only to compare hours
                            minutes_folder_datetime = datetime.strptime(daily_folder + "_" + minutes_folder,
                                                                        "%d-%m-%y_%H_%M_%S")
                            log_max_hours = os.getenv("alyvix_log_max_hours_per_days", "24")
                            log_max_hours_int = int(log_max_hours)

                            #if hours folder are too old, then delete it
                            if minutes_folder_datetime <= current_datetime + timedelta(hours=(log_max_hours_int * -1)):
                                shutil.rmtree(current_full_path + os.sep + minutes_folder)
                        except:
                            pass
            except:
                pass

    def __delete_items_but(self, path, max_items=None, exclude_item=None):
        if max_items is not None:

            if not os.path.exists(path):
                return

            items = os.listdir(path)

            if exclude_item is not None:
                new_items = [item for item in items if item != exclude_item]
            else:
                new_items = items

            while len(new_items) >= max_items:
            
                try:

                    items = os.listdir(path)

                    if exclude_item is not None:
                        new_items = [item for item in items if item != exclude_item]
                    else:
                        new_items = items

                    full_name = path + os.sep + new_items[0]

                    if os.path.isdir(full_name) is True:
                        shutil.rmtree(full_name)
                    else:
                        os.remove(full_name)
                        
                except:
                    pass

    def __delete_files(self, path, max_files=None):

        if not os.path.exists(path):
            return

        if max_files is not None:
            while len(os.listdir(path)) > max_files:
            
                try:

                    files = os.listdir(path)
                    os.remove(path + os.sep + files[0])
                except:
                    pass

    def is_log_enable(self):
        """
        check if log is enabled.

        :rtype: bool
        :return: a boolean
        """
        return self.__enable_log

    def save_exception(self, type, text_data):

        """
        save the exception into the test case log folder.

        :type sub_dir: string
        :param sub_dir: the subdir to create inside the test case log folder
        :type file_name: string
        :param file_name: the name of the image
        :type text_data: string
        :param text_data: the text to write
        """

        global log_path

        if self.__enable_log is True:

            current_time = datetime.now().strftime("%H:%M:%S.%f")

            fullname = log_path + os.sep + "log.txt"
            #print fullname

            if not os.path.exists(log_path):
                os.makedirs(log_path)

            text_file = open(fullname, "a")
            text_file.write(type + "|" + current_time + "|" + self.__name + "|" + text_data)
            text_file.close()

    def save_image(self, sub_dir, image_name, image_data):
        """
        save the image into the test case log folder.

        :type sub_dir: string
        :param sub_dir: the subdir to create inside the test case log folder
        :type image_name: string
        :param image_name: the name of the image
        :type image_data: numpy.ndarray or Image.Image or cv.iplimage
        :param image_data: the image data to save
        """

        try:
            if isinstance(image_data, numpy.ndarray):
                self.__save_numpy_image(sub_dir, image_name, image_data)

            if isinstance(image_data, Image.Image):
                self.__save_pil_image(sub_dir, image_name, image_data)

            if isinstance(image_data, cv.iplimage):
                self.__save_cv_image(sub_dir, image_name, image_data)
        except Exception, err:
            pass
예제 #3
0
class PerfManager:
    def __init__(self):
        self.performance_desc_string = ""
        self._info_manager = InfoManager()
        self._info_manager.tiny_update()

        self._last_filled_perf = None

    def clear_perfdata(self):
        global perfdata_list
        perfdata_list = []

    def add_perfdata(self,
                     name,
                     value=None,
                     warning_threshold=None,
                     critical_threshold=None,
                     state=0):

        global perfdata_list
        global perf_counter

        perf_data = _PerfData()
        perf_data.name = str(name)

        try:
            perf_data.value = float(value)
            if perf_data.counter == -1:
                perf_data.counter = perf_counter
                perf_counter = perf_counter + 1
        except:
            perf_data.value = ""

        try:
            perf_data.warning_threshold = float(warning_threshold)
        except:
            perf_data.warning_threshold = ""

        try:
            perf_data.critical_threshold = float(critical_threshold)
        except:
            perf_data.critical_threshold = ""

        try:
            perf_data.state = int(state)
        except:
            perf_data.state = 3

        cnt = 0

        for perf_data_in_list in perfdata_list:
            if perf_data_in_list.name == perf_data.name:

                perfdata_list[cnt] = perf_data
                return

            cnt = cnt + 1

        perfdata_list.append(perf_data)

    def rename_perfdata(self,
                        old_name,
                        new_name,
                        warning_threshold="",
                        critical_threshold=""):

        global perfdata_list

        perfdata_list_copy = copy.deepcopy(perfdata_list)

        cnt = 0
        for perf_data_in_list in perfdata_list:
            if perf_data_in_list.name == str(new_name):
                del perfdata_list_copy[cnt]
                cnt = cnt - 1
            elif perf_data_in_list.name == str(old_name):

                perfdata_list_copy[cnt].name = str(new_name)

                try:
                    new_warning_threshold = float(warning_threshold)
                    perfdata_list_copy[
                        cnt].warning_threshold = new_warning_threshold
                except:
                    pass

                try:
                    new_critical_threshold = float(critical_threshold)
                    perfdata_list_copy[
                        cnt].critical_threshold = new_critical_threshold
                except:
                    pass

            cnt = cnt + 1

        perfdata_list = copy.deepcopy(perfdata_list_copy)

    def get_perfdata(self, name, delete_perfdata=False):

        global perfdata_list
        ret_val = None
        perfdata_list_copy = copy.deepcopy(perfdata_list)

        cnt = 0
        for perf_data_in_list in perfdata_list:
            if perf_data_in_list.name == name:

                if perf_data_in_list.value == "" or perf_data_in_list.value is None:
                    raise Exception('Perf data value is Null')

                if delete_perfdata is True:
                    del perfdata_list_copy[cnt]

                ret_val = perf_data_in_list.value
            cnt = cnt + 1

        perfdata_list = copy.deepcopy(perfdata_list_copy)
        perfdata_list_copy = []
        return ret_val

    def get_all_perfdata(self):
        global perfdata_list
        return copy.deepcopy(perfdata_list)

    def delete_perfdata(self, name):

        global perfdata_list

        perfdata_list_copy = copy.deepcopy(perfdata_list)

        cnt = 0
        for perf_data_in_list in perfdata_list:
            if perf_data_in_list.name == name:

                del perfdata_list_copy[cnt]

            cnt = cnt + 1

        perfdata_list = copy.deepcopy(perfdata_list_copy)
        perfdata_list_copy = []

    def sum_perfdata(self, *names, **kwargs):

        global perfdata_list

        sum = None

        value_to_sum = []
        index_to_delete = []
        perfdata_list_copy = []

        delete_perf = False
        perf_name = ""
        warning_threshold = None
        critical_threshold = None

        try:
            delete_perf = kwargs['delete_perfdata']
        except:
            pass

        try:
            perf_name = kwargs['name']
        except:
            pass

        try:
            warning_threshold = float(kwargs['warning_threshold'])
        except:
            pass

        try:
            critical_threshold = float(kwargs['critical_threshold'])
        except:
            pass

        cnt = 0
        for perf_data_in_list in perfdata_list:
            for name in names:
                if perf_data_in_list.name == name and perf_data_in_list.value != ""\
                        and perf_data_in_list.value is not None:
                    value_to_sum.append(perf_data_in_list.value)
                    sum = 0  #init sum

                    if delete_perf is True:
                        index_to_delete.append(cnt)

                elif perf_data_in_list.name == name and (perf_data_in_list.value == ""\
                        or perf_data_in_list.value is None):
                    raise Exception('You cannot sum empty value(s)')
                """
                elif (delete_perf is True or perf_name != "") and perf_data_in_list.name == name:
                    index_to_delete.append(cnt)
                """

            cnt = cnt + 1

        cnt = 0
        for perf in perfdata_list:

            if cnt not in index_to_delete:
                perfdata_list_copy.append(perf)

            cnt = cnt + 1

        perfdata_list = copy.deepcopy(perfdata_list_copy)
        perfdata_list_copy = []

        for perf in value_to_sum:

            sum = sum + perf

        if perf_name != "":
            self.add_perfdata(perf_name, sum, warning_threshold,
                              critical_threshold)

        return sum

    def order_perfdata(self):
        global perfdata_list
        perfdata_ok_list = []
        perfdata_notok_list = []

        for perf_data_in_list in perfdata_list:

            if perf_data_in_list.counter != -1:
                perfdata_ok_list.append(copy.deepcopy(perf_data_in_list))
            else:
                perfdata_notok_list.append(copy.deepcopy(perf_data_in_list))

        perfdata_ok_list.sort(key=lambda x: x.counter, reverse=False)

        if len(perfdata_ok_list) > 0:
            self._last_filled_perf = perfdata_ok_list[-1].name

        perfdata_list = []
        perfdata_list = perfdata_ok_list + perfdata_notok_list

    def get_perfdata_string(self):

        global perfdata_list

        ret_string = ""
        cnt = 0
        for perfdata in perfdata_list:

            name = perfdata.name
            value = perfdata.value
            warning = perfdata.warning_threshold
            critical = perfdata.critical_threshold

            if cnt == 0:
                ret_string = ret_string + name + "=" + str(value) + "s;" + str(
                    warning) + ";" + str(critical) + ";;"
            else:
                ret_string = ret_string + " " + name + "=" + str(
                    value) + "s;" + str(warning) + ";" + str(critical) + ";;"

            cnt = cnt + 1

        return ret_string

    def get_output(self, message=None, print_output=True):

        prefix_robot_framework = ""

        global perfdata_list
        global timedout_finders

        self.order_perfdata()

        exitcode = self.get_exitcode()
        performanceData = self.get_perfdata_string()

        if performanceData is not "":
            performanceData = "|" + performanceData
        else:
            performanceData = ""

        if message is not None:
            self.performance_desc_string = self.performance_desc_string + message + performanceData + os.linesep
        elif exitcode == 2:
            self.performance_desc_string = self.performance_desc_string +\
                                           "CRITICAL: one or more steps are in critical state" +\
                                           performanceData + os.linesep
            prefix_robot_framework = "*WARN*"
        elif exitcode == 1:
            self.performance_desc_string = self.performance_desc_string +\
                                           "WARNING: one or more steps are in warning state" +\
                                           performanceData + os.linesep
            prefix_robot_framework = "*WARN*"
        elif exitcode == 3:
            if self._last_filled_perf is not None:
                self.performance_desc_string = self.performance_desc_string +\
                                               "UNKNOWN: some error occurred, last filled perf data is " +\
                                               self._last_filled_perf + " " + performanceData + os.linesep
            else:
                self.performance_desc_string = self.performance_desc_string +\
                                               "UNKNOWN: some error occurred, no perf data was filled" +\
                                               performanceData + os.linesep
            prefix_robot_framework = "*WARN*"
        elif len(timedout_finders) > 0:
            self.performance_desc_string = self.performance_desc_string +\
                                           "CRITICAL: one or more steps are in timeout state" +\
                                           performanceData + os.linesep
            prefix_robot_framework = "*WARN*"
        else:
            self.performance_desc_string = self.performance_desc_string +\
                                           "OK: all steps are ok" +\
                                           performanceData + os.linesep

        for perfdata in perfdata_list:

            name = perfdata.name
            value = perfdata.value
            warning = perfdata.warning_threshold
            critical = perfdata.critical_threshold
            #state = perfdata.state

            #only for Alyvix
            state = 3
            if value != "" and critical != "" and value >= critical:
                state = 2
            elif value != "" and warning != "" and value >= warning:
                state = 1
            elif value != "":
                state = 0
            elif value == "" and warning == "" and critical == "" and state == 0:
                state = 3

            if state == 0:
                self.performance_desc_string = self.performance_desc_string +\
                                               "OK: " + name + " time is " + str(value) + " sec." + os.linesep
            elif state == 1:
                self.performance_desc_string = self.performance_desc_string +\
                                               "WARNING: " + name + " time is " + str(value) + " sec." + os.linesep
            elif state == 2:
                self.performance_desc_string = self.performance_desc_string +\
                                               "CRITICAL: " + name + " time is " + str(value) + " sec." +\
                                               os.linesep
            else:
                if value != "":
                    self.performance_desc_string = self.performance_desc_string +\
                                                   "UNKNOWN: " + name + " time is " + str(value) + " sec." + os.linesep
                elif value == "":
                    self.performance_desc_string = self.performance_desc_string +\
                                                   "UNKNOWN: " + name + " time is null." + os.linesep

        os.environ["alyvix_exitcode"] = str(exitcode)
        os.environ["alyvix_std_output"] = self.performance_desc_string

        if self._info_manager.get_info("ROBOT CONTEXT") is True:

            suite_source = self._info_manager.get_info("SUITE SOURCE")

            file_name = suite_source.split(os.sep)[-1].split(".")[0]

            result_dir = tempfile.gettempdir(
            ) + os.sep + "alyvix_pybot" + os.sep + file_name + os.sep + "result"
        else:
            result_dir = "."

        if not os.path.exists(result_dir):
            os.makedirs(result_dir)

        text_file = open(result_dir + os.sep + "message.txt", "w")
        text_file.write(self.performance_desc_string)
        text_file.close()

        text_file = open(result_dir + os.sep + "exitcode.txt", "w")
        text_file.write(str(exitcode))
        text_file.close()

        if print_output is True:
            print prefix_robot_framework + self.performance_desc_string
        return exitcode

    def get_exitcode(self):

        global perfdata_list
        exitcode = 0

        for perfdata in perfdata_list:

            name = perfdata.name
            value = perfdata.value
            warning = perfdata.warning_threshold
            critical = perfdata.critical_threshold
            state = perfdata.state

            #only for Alyvix
            if value != "" and critical != "" and value >= critical:
                state = 2
            elif value != "" and warning != "" and value >= warning:
                state = 1
            elif value == "" and warning == "" and critical == "" and state == 0:
                state = 3

            if state > exitcode:
                exitcode = state

            if exitcode == 2:
                break

        return exitcode
예제 #4
0
class PerfManager:

    def __init__(self):
        self.performance_desc_string = ""
        self._info_manager = InfoManager()
        self._info_manager.tiny_update()

        self._last_filled_perf = None
        self.not_ok_perfdata = 0

    def clear_perfdata(self):
        global perfdata_list
        perfdata_list = []


    def add_perfdata(self, name, value=None, warning_threshold=None, critical_threshold=None, state=None, inittimestamp=False):

        global perfdata_list
        global perf_counter
        global declaration_counter

        declaration_counter = declaration_counter + 1

        perf_data = _PerfData()
        perf_data.name = str(name)

        try:
            perf_data.value = float(value)
            if perf_data.counter == -1:
                perf_data.counter = perf_counter
                perf_counter = perf_counter + 1
        except:
            perf_data.value = ""

        try:
            perf_data.warning_threshold = float(warning_threshold)
        except:
            perf_data.warning_threshold = ""

        try:
            perf_data.critical_threshold = float(critical_threshold)
        except:
             perf_data.critical_threshold = ""


        if state is not None:
            try:
                perf_data.state = int(state)
            except:
                perf_data.state = 2 #3
        else:
            try:
                perf_data.state = int(os.getenv("exitcode"))
            except:
                perf_data.state = 2 #3

        if perf_data.value != "" and perf_data.critical_threshold != "" and perf_data.value >= perf_data.critical_threshold:
            perf_data.state = 2
        elif perf_data.value != "" and perf_data.warning_threshold != "" and perf_data.value >= perf_data.warning_threshold:
            perf_data.state = 1
        elif perf_data.value != "":
            perf_data.state = 0

        initts = False

        if inittimestamp == True:
            initts = True

        try:
            if str(inittimestamp).lower() == "true":
                initts = True
        except:
            pass

        if  initts == True:
            perf_data.timestamp = int(time.time() * 1000)

            current_keyword_timestamp_array = self._info_manager.get_info('KEYWORD TIMESTAMP')

            # current_keyword_timestamp_array_copy = copy.deepcopy(current_keyword_timestamp_array)

            timestamp_modified = False
            for cnt_kts in xrange(len(current_keyword_timestamp_array)):
                if current_keyword_timestamp_array[cnt_kts][0] == name:
                    timestamp_modified = True
                    current_keyword_timestamp_array[cnt_kts] = (name, perf_data.timestamp)
                    break

            if timestamp_modified is False:
                current_keyword_timestamp_array.append((name, perf_data.timestamp))

            self._info_manager.set_info('KEYWORD TIMESTAMP',
                                        current_keyword_timestamp_array)

        else:
            perf_data.timestamp = None

        keywords_timestamp_array = self._info_manager.get_info('KEYWORD TIMESTAMP')

        for cnt_kts in xrange(len(keywords_timestamp_array)):
            if keywords_timestamp_array[cnt_kts][0] == name:
                perf_data.timestamp = keywords_timestamp_array[cnt_kts][1]
                break

        perf_data.timeout_threshold = None

        keywords_timeout_array = self._info_manager.get_info('KEYWORD TIMEOUT')

        for cnt_ktout in xrange(len(keywords_timeout_array)):
            if keywords_timeout_array[cnt_ktout][0] == name:
                perf_data.timeout_threshold = keywords_timeout_array[cnt_ktout][1]
                break

        cnt = 0

        for perf_data_in_list in perfdata_list:
            if perf_data_in_list.name == perf_data.name:
                perf_data.custom_tags = perfdata_list[cnt].custom_tags
                perf_data.custom_fields = perfdata_list[cnt].custom_fields

                perfdata_list[cnt] = perf_data
                return

            cnt = cnt + 1

        perfdata_list.append(perf_data)

    def rename_perfdata(self, old_name, new_name, warning_threshold="", critical_threshold=""):

        global perfdata_list

        perfdata_list_copy = copy.deepcopy(perfdata_list)

        for perf_data_in_list in perfdata_list:
            if perf_data_in_list.name == str(old_name):
                if perf_data_in_list.value == "":
                    raise Exception("The Keyword value is None")

        keywords_timestamp_array = copy.deepcopy(self._info_manager.get_info('KEYWORD TIMESTAMP'))

        cnt = 0

        for cnt_kts in xrange(len(keywords_timestamp_array)):
            if keywords_timestamp_array[cnt_kts][0] == str(new_name):
                del keywords_timestamp_array[cnt_kts]

        old_name_exists = False
        for perf_data_in_list in perfdata_list:

            for cnt_kts in xrange(len(keywords_timestamp_array)):
                if keywords_timestamp_array[cnt_kts][0] == str(old_name):
                    keywords_timestamp_array[cnt_kts] = (new_name, keywords_timestamp_array[cnt_kts][1])

            self._info_manager.set_info('KEYWORD TIMESTAMP', keywords_timestamp_array)

            if perf_data_in_list.name == str(new_name):
                deleted_on_rename_list.append(copy.deepcopy(perfdata_list_copy[cnt]))
                del perfdata_list_copy[cnt]
                cnt = cnt - 1
            elif perf_data_in_list.name == str(old_name):

                old_name_exists = True

                perfdata_list_copy[cnt].name = str(new_name)

                try:
                    new_warning_threshold = float(warning_threshold)
                    perfdata_list_copy[cnt].warning_threshold = new_warning_threshold
                except:
                    pass

                try:
                    new_critical_threshold = float(critical_threshold)
                    perfdata_list_copy[cnt].critical_threshold = new_critical_threshold
                except:
                    pass

            cnt = cnt + 1

        if old_name_exists is False:
            raise Exception("The keyword name does not exist")

        perfdata_list = copy.deepcopy(perfdata_list_copy)



        cnt = 0
        scraper_list_copy = copy.deepcopy(self._info_manager.get_info('SCRAPER COLLECTION'))

        for sc_in_list in self._info_manager.get_info('SCRAPER COLLECTION'):

            name = sc_in_list[0]

            if name == old_name:
                scraper_list_copy[cnt] = (new_name, sc_in_list[1], sc_in_list[2])

            cnt += 1

        self._info_manager.set_info('SCRAPER COLLECTION', copy.deepcopy(scraper_list_copy))


    def set_perfdata_extra(self, name, extra):

        global perfdata_list

        for perf_data_in_list in perfdata_list:
            if perf_data_in_list.name == str(name):
                perf_data_in_list.extra = str(extra)

    def add_perfdata_tag(self, perf_name, tag_name, tag_value):

        global perfdata_list

        keyword_exist = False

        for perf_data_in_list in perfdata_list:
            if perf_data_in_list.name == str(perf_name) or str(perf_name) == "all":
                perf_data_in_list.custom_tags[str(tag_name)] = str(tag_value)
                keyword_exist = True

        if keyword_exist is False:
            raise Exception("The keyword name does not exist")

    def add_perfdata_field(self, perf_name, field_name, field_value):

        global perfdata_list

        keyword_exist = False

        for perf_data_in_list in perfdata_list:
            if perf_data_in_list.name == str(perf_name) or str(perf_name) == "all":
                perf_data_in_list.custom_fields[str(field_name)] = str(field_value)
                keyword_exist = True

        if keyword_exist is False:
            raise Exception("The keyword name does not exist")

    def get_perfdata(self, name, delete_perfdata=False):

        keyword_exist = False

        global perfdata_list
        ret_val = None
        perfdata_list_copy = copy.deepcopy(perfdata_list)

        cnt = 0
        for perf_data_in_list in perfdata_list:
            if perf_data_in_list.name == name:

                keyword_exist = True

                if perf_data_in_list.value == "" or perf_data_in_list.value is None:
                    raise Exception('The performance measure is None')

                if delete_perfdata is True:
                    del perfdata_list_copy[cnt]

                ret_val = perf_data_in_list.value
            cnt = cnt + 1

        perfdata_list = copy.deepcopy(perfdata_list_copy)
        perfdata_list_copy = []

        if keyword_exist is False:
            raise Exception("The keyword name does not exist")

        return ret_val

    def get_all_perfdata(self):
        global perfdata_list
        return copy.deepcopy(perfdata_list)

    def delete_perfdata(self, name):

        global perfdata_list

        perfdata_list_copy = copy.deepcopy(perfdata_list)

        cnt = 0
        for perf_data_in_list in perfdata_list:
            if perf_data_in_list.name == name:

                del perfdata_list_copy[cnt]

            cnt = cnt + 1

        perfdata_list = copy.deepcopy(perfdata_list_copy)
        perfdata_list_copy = []

    def sum_perfdata(self, *names, **kwargs):

        global perfdata_list

        sum = None

        value_to_sum = []
        index_to_delete = []
        perfdata_list_copy = []

        delete_perf = False
        perf_name = ""
        warning_threshold  = None
        critical_threshold = None

        try:
            delete_perf = kwargs['delete_perfdata']
        except:
            pass

        try:
            perf_name = kwargs['name']
        except:
            pass

        try:
            warning_threshold = float(kwargs['warning_threshold'])
        except:
            pass

        try:
            critical_threshold = float(kwargs['critical_threshold'])
        except:
            pass

        biggest_timestamp = 0
        smallest_timestamp = 10413792000000 #2300/01/01
        value_of_last_perf = None
        timeout_of_last_perf = None

        all_name_exist = True




        for name in names:
            name_exists = False
            for perf_data_in_list in perfdata_list:
                if perf_data_in_list.name == name:
                    name_exists = True

            if name_exists is False:
                raise Exception("The keyword name (" + name + ") does not exist")

        cnt = 0
        for perf_data_in_list in perfdata_list:

            for name in names:

                if perf_data_in_list.name == name and perf_data_in_list.value != ""\
                        and perf_data_in_list.value is not None:
                    value_to_sum.append(perf_data_in_list.value)
                    sum = 0 #init sum

                    if perf_data_in_list.timestamp is None:
                        raise Exception(name + ": The performance timestamp is None")

                    if perf_data_in_list.timestamp < smallest_timestamp:
                        smallest_timestamp = perf_data_in_list.timestamp

                    if perf_data_in_list.timestamp > biggest_timestamp:
                        biggest_timestamp = perf_data_in_list.timestamp
                        value_of_last_perf = perf_data_in_list.value
                        timeout_of_last_perf = perf_data_in_list.timeout_threshold

                    if delete_perf is True:
                        index_to_delete.append(cnt)

                elif perf_data_in_list.name == name and (perf_data_in_list.value == ""\
                        or perf_data_in_list.value is None):
                    raise Exception("The performance measure (" + name + ") is None")

            cnt = cnt + 1

        cnt = 0
        for perf in perfdata_list:

            if cnt not in index_to_delete:
                perfdata_list_copy.append(perf)

            cnt = cnt + 1

        perfdata_list = copy.deepcopy(perfdata_list_copy)
        perfdata_list_copy = []

        for perf in value_to_sum:

            sum = sum + perf

        if perf_name != "":

            self.add_perfdata(perf_name, sum, warning_threshold, critical_threshold)

            for perf in perfdata_list:
                if perf.name == perf_name:

                    perf.timestamp = smallest_timestamp

                    current_keyword_timestamp_array = self._info_manager.get_info('KEYWORD TIMESTAMP')

                    # current_keyword_timestamp_array_copy = copy.deepcopy(current_keyword_timestamp_array)

                    timestamp_modified = False
                    for cnt_kts in xrange(len(current_keyword_timestamp_array)):
                        if current_keyword_timestamp_array[cnt_kts][0] == perf_name:
                            timestamp_modified = True
                            current_keyword_timestamp_array[cnt_kts] = (perf_name, smallest_timestamp)
                            break

                    if timestamp_modified is False:
                        current_keyword_timestamp_array.append((perf_name, smallest_timestamp))

                    self._info_manager.set_info('KEYWORD TIMESTAMP',
                                                current_keyword_timestamp_array)

                    try:
                        end_timestamp_only_for_summed_perf = (float(biggest_timestamp)/1000) + value_of_last_perf
                        perf.end_timestamp_only_for_summed_perf = int(end_timestamp_only_for_summed_perf*1000)
                    except:
                        try:
                            end_timestamp_only_for_summed_perf = (float(biggest_timestamp)/1000) + timeout_of_last_perf
                            perf.end_timestamp_only_for_summed_perf = int(end_timestamp_only_for_summed_perf*1000)
                        except:
                            perf.end_timestamp_only_for_summed_perf = biggest_timestamp

        return sum

    def get_last_filled(self):
        #self.order_perfdata()
        return self._last_filled_perf

    def order_perfdata(self):
        global perfdata_list
        perfdata_ok_list = []
        perfdata_notok_list = []

        for perf_data_in_list in perfdata_list:

            if perf_data_in_list.counter != -1:
                perfdata_ok_list.append(copy.deepcopy(perf_data_in_list))
            else:
                perfdata_notok_list.append(copy.deepcopy(perf_data_in_list))
                self.not_ok_perfdata = self.not_ok_perfdata + 1

        perfdata_ok_list.sort(key=lambda x: x.counter, reverse=False)

        if len(perfdata_ok_list) > 0:
            self._last_filled_perf = perfdata_ok_list[-1].name

        perfdata_list = []
        perfdata_list = perfdata_ok_list + perfdata_notok_list

    def get_perfdata_string(self):

        global perfdata_list

        ret_string = ""
        cnt = 0
        for perfdata in perfdata_list:

            name = perfdata.name

            if perfdata.value == '' or perfdata.value is None:
                value = ''
            else:
                value = ("%.3f" % perfdata.value)

            if perfdata.warning_threshold == '' or perfdata.warning_threshold is None:
                warning = ''
            else:
                warning = ("%.3f" % perfdata.warning_threshold)

            if perfdata.critical_threshold == '' or perfdata.critical_threshold is None:
                critical = ''
            else:
                critical = ("%.3f" % perfdata.critical_threshold)

            if cnt == 0:
                ret_string = ret_string + name + "=" + value + "s;" + warning + ";" + critical + ";;"
            else:
                ret_string = ret_string + " " + name + "=" + value + "s;" + warning + ";" + critical + ";;"

            cnt = cnt + 1

        return ret_string.replace("=s;;;;","=;;;;")

    def get_output(self, message=None, print_output=True):

        prefix_robot_framework = ""

        global perf_counter
        global declaration_counter
        global perfdata_list
        global timedout_finders

        self.order_perfdata()

        exitcode = self.get_exitcode()
        performanceData = self.get_perfdata_string()

        if performanceData is not "":
            performanceData = "|" + performanceData
        else:
            performanceData = ""

        if self._info_manager.get_info("RESOLUTION BGS OK") is False:
            self.performance_desc_string = self.performance_desc_string + \
               "Alyvix Background Service is installed but the screen resolution doesn't match with the config file"\
               + performanceData + os.linesep

        elif message is not None:
            self.performance_desc_string = self.performance_desc_string + message + performanceData + os.linesep
        elif exitcode == 3 and self.not_ok_perfdata == len(perfdata_list):
            self.performance_desc_string = self.performance_desc_string + \
                                           "UNKNOWN: some error occurred, no measure was taken" + \
                                           performanceData + os.linesep
            prefix_robot_framework = "*WARN*"
        elif exitcode == 3 and self.not_ok_perfdata > 0:
            self.performance_desc_string = self.performance_desc_string + \
                                           "UNKNOWN: one transaction breaks the test case, the last received measure is " + \
                                           self._last_filled_perf + " " + performanceData + os.linesep
            prefix_robot_framework = "*WARN*"
        elif exitcode == 2 and self.not_ok_perfdata == len(perfdata_list):
            self.performance_desc_string = self.performance_desc_string + \
                                           "CRITICAL: some error occurred, no measure was taken" + \
                                           performanceData + os.linesep
            prefix_robot_framework = "*WARN*"
        elif exitcode == 2 and self.not_ok_perfdata > 0:
            self.performance_desc_string = self.performance_desc_string +\
                                               "CRITICAL: one transaction breaks the test case, the last received measure is " +\
                                               self._last_filled_perf + " " + performanceData + os.linesep
            prefix_robot_framework = "*WARN*"
        elif exitcode == 2:
            self.performance_desc_string = self.performance_desc_string +\
                                           "CRITICAL: one or more transactions run critical" +\
                                           performanceData + os.linesep
            prefix_robot_framework = "*WARN*"
        elif exitcode == 1 and self.not_ok_perfdata == len(perfdata_list):
            self.performance_desc_string = self.performance_desc_string + \
                                           "WARNING: some error occurred, no measure was taken" + \
                                           performanceData + os.linesep
            prefix_robot_framework = "*WARN*"
        elif exitcode == 1 and self.not_ok_perfdata > 0:
            self.performance_desc_string = self.performance_desc_string +\
                                               "WARNING: one transaction breaks the test case, the last received measure is " +\
                                               self._last_filled_perf + " " + performanceData + os.linesep
            prefix_robot_framework = "*WARN*"
        elif exitcode == 1:
            self.performance_desc_string = self.performance_desc_string +\
                                           "WARNING: one or more transactions run in warning" +\
                                           performanceData + os.linesep
            prefix_robot_framework = "*WARN*"
        elif exitcode == 0 and self.not_ok_perfdata == len(perfdata_list):
            self.performance_desc_string = self.performance_desc_string + \
                                           "Ok: some error occurred, no measure was taken" + \
                                           performanceData + os.linesep
        elif exitcode == 0 and self.not_ok_perfdata > 0:
            self.performance_desc_string = self.performance_desc_string +\
                                               "OK: one transaction breaks the test case, the last received measure is " +\
                                               self._last_filled_perf + " " + performanceData + os.linesep
        else:
            self.performance_desc_string = self.performance_desc_string +\
                                           "OK: all transactions run ok" +\
                                           performanceData + os.linesep

        if declaration_counter == 0 and perf_counter == 0:
            self.performance_desc_string = self.performance_desc_string.replace("some error occurred, no measure was taken",
                                                                                "no transaction is declared")

        for perfdata in perfdata_list:

            name = perfdata.name

            if perfdata.value != "" and perfdata.critical_threshold != "" and perfdata.value >= perfdata.critical_threshold:
                perfdata.state = 2
            elif perfdata.value != "" and perfdata.warning_threshold != "" and perfdata.value >= perfdata.warning_threshold:
                perfdata.state = 1
            elif perfdata.value != "":
                perfdata.state = 0

            state = perfdata.state

            if perfdata.value == '' or perfdata.value is None:
                value = perfdata.value
            else:
                value = ("%.3f" % perfdata.value)


            if state == 0 and value == "":
                self.performance_desc_string = self.performance_desc_string +\
                                               "OK: " + name + " measures None" + os.linesep
            elif state == 0:
                self.performance_desc_string = self.performance_desc_string +\
                                               "OK: " + name + " measures " + value + "s" + os.linesep
            elif state == 1 and value == "":
                self.performance_desc_string = self.performance_desc_string +\
                                               "WARNING: " + name + " measures None" + os.linesep
            elif state == 1:
                self.performance_desc_string = self.performance_desc_string +\
                                               "WARNING: " + name + " measures " + value + "s" + os.linesep
            elif state == 2 and value == "":
                self.performance_desc_string = self.performance_desc_string +\
                                               "CRITICAL: " + name + " measures None" + os.linesep
            elif state == 2:
                self.performance_desc_string = self.performance_desc_string + \
                                               "CRITICAL: " + name + " measures " + value + "s" + \
                                               os.linesep
            else:
                self.performance_desc_string = self.performance_desc_string +\
                                                   "UNKNOWN: " + name + " time is null." + os.linesep


        if self._info_manager.get_info("ROBOT CONTEXT") is True:

            suite_source = self._info_manager.get_info("SUITE SOURCE")

            file_name = suite_source.split(os.sep)[-1].split(".")[0]

            result_dir = tempfile.gettempdir() + os.sep + "alyvix_pybot" + os.sep + file_name + os.sep + "result"
        else:
            result_dir = "."

        if not os.path.exists(result_dir):
            os.makedirs(result_dir)

        text_file = open(result_dir + os.sep + "message.txt", "w")
        text_file.write(self.performance_desc_string)
        text_file.close()

        text_file = open(result_dir + os.sep + "exitcode.txt", "w")
        text_file.write(str(exitcode))
        text_file.close()

        if print_output is True:
            print prefix_robot_framework + self.performance_desc_string
        return exitcode

    def get_exitcode(self):

        global perfdata_list

        exitcode = 0

        not_ok_exitcode = None

        for perfdata in perfdata_list:

            if perfdata.value is None or perfdata.value == "":
                if not_ok_exitcode is None:
                    not_ok_exitcode = perfdata.state
                elif perfdata.state > not_ok_exitcode:
                    not_ok_exitcode = perfdata.state


        if len(perfdata_list) == 0 and len(deleted_on_rename_list) != 0:
            perfdata_list = copy.deepcopy(deleted_on_rename_list)

        for perfdata in perfdata_list:

            if perfdata.value is None or perfdata.value == "":
                if perfdata.state > exitcode:
                    exitcode = perfdata.state
            if perfdata.critical_threshold is not None and perfdata.critical_threshold != "":
                if perfdata.value >= int(perfdata.critical_threshold):
                    if 2 > exitcode:
                        exitcode = 2
            if perfdata.warning_threshold is not None and perfdata.warning_threshold != "":
                if perfdata.value >= int(perfdata.warning_threshold):
                    if 1 > exitcode:
                        exitcode = 1

            """
            state = perfdata.state

            if state == 0 and self.not_ok_perfdata == 0:
                #we are in the init step
                if exitcode == 2: #3
                    exitcode = 0
            elif state == 1 or state == 2:
                if exitcode == 2: #3
                    exitcode = state
                elif state > exitcode:
                    exitcode = state

            if exitcode == 2:
                break
            """

        if not_ok_exitcode != None and self.not_ok_perfdata > 0:
            exitcode = not_ok_exitcode

        if self.not_ok_perfdata > 0:
            try:
                exitcode = int(os.getenv("exitcode"))
            except:
                pass

        return exitcode