<commit_before>'''
Check the performance counters from IIS
'''
from checks import AgentCheck

class IIS(AgentCheck):
    METRICS = [
        ('iis.uptime', 'gauge', 'ServiceUptime'),

        # Network
        ('iis.net.bytes_sent', 'gauge', 'BytesSentPerSec'),
        ('iis.net.bytes_rcvd', 'gauge', 'BytesReceivedPerSec'),
        ('iis.net.bytes_total', 'gauge', 'BytesTotalPerSec'),
        ('iis.net.num_connections', 'gauge', 'CurrentConnections'),
        ('iis.net.files_sent', 'gauge', 'FilesSentPerSec'),
        ('iis.net.files_rcvd', 'gauge', 'FilesReceivedPerSec'),
        ('iis.net.connection_attempts', 'gauge', 'ConnectionAttemptsPerSec'),

        # HTTP Methods
        ('iis.httpd_request_method.get', 'gauge', 'GetRequestsPerSec'),
        ('iis.httpd_request_method.post', 'gauge', 'PostRequestsPerSec'),
        ('iis.httpd_request_method.head', 'gauge', 'HeadRequestsPerSec'),
        ('iis.httpd_request_method.put', 'gauge', 'PutRequestsPerSec'),
        ('iis.httpd_request_method.delete', 'gauge', 'DeleteRequestsPerSec'),
        ('iis.httpd_request_method.options', 'gauge', 'OptionsRequestsPerSec'),
        ('iis.httpd_request_method.trace', 'gauge', 'TraceRequestsPerSec'),

        # Errors
        ('iis.errors.not_found', 'gauge', 'NotFoundErrorsPerSec'),
        ('iis.errors.locked', 'gauge', 'LockedErrorsPerSec'),

        # Users
        ('iis.users.anon', 'gauge', 'AnonymousUsersPerSec'),
        ('iis.users.nonanon', 'gauge', 'NonAnonymousUsersPerSec'),

        # Requests
        ('iis.requests.cgi', 'gauge', 'CGIRequestsPerSec'),
        ('iis.requests.isapi', 'gauge', 'ISAPIExtensionRequestsPerSec'),
    ]

    def check(self, instance):
        try:
            import wmi
        except ImportError:
            self.log.error("Unable to import 'wmi' module")
            return

        # Connect to the WMI provider
        host = instance.get('host', None)
        user = instance.get('username', None)
        password = instance.get('password', None)
        tags = instance.get('tags', None)
        w = wmi.WMI(host, user=user, password=password)

        try:
            wmi_cls = w.Win32_PerfFormattedData_W3SVC_WebService(name="_Total")
            if not wmi_cls:
                raise Exception('Missing _Total from Win32_PerfFormattedData_W3SVC_WebService')
        except Exception:
            self.log.exception('Unable to fetch Win32_PerfFormattedData_W3SVC_WebService class')
            return

        wmi_cls = wmi_cls[0]
        for metric, mtype, wmi_val in self.METRICS:
            if not hasattr(wmi_cls, wmi_val):
                self.log.error('Unable to fetch metric %s. Missing %s in Win32_PerfFormattedData_W3SVC_WebService' \
                    % (metric, wmi_val))
                continue
            value = getattr(wmi_cls, wmi_val)
            self.gauge(metric, value, tags=tags)<commit_msg>IIS: Use total values from WMI and calculate rates in the agent<commit_after>'''
Check the performance counters from IIS
'''
from checks import AgentCheck

class IIS(AgentCheck):
    METRICS = [
        ('iis.uptime', 'gauge', 'ServiceUptime'),

        # Network
        ('iis.net.bytes_sent', 'gauge', 'BytesSentPerSec'),
        ('iis.net.bytes_rcvd', 'gauge', 'BytesReceivedPerSec'),
        ('iis.net.bytes_total', 'gauge', 'BytesTotalPerSec'),
        ('iis.net.num_connections', 'gauge', 'CurrentConnections'),
        ('iis.net.files_sent', 'rate', 'TotalFilesSent'),
        ('iis.net.files_rcvd', 'rate', 'TotalFilesReceived'),
        ('iis.net.connection_attempts', 'rate', 'TotalConnectionAttemptsAllInstances'),

        # HTTP Methods
        ('iis.httpd_request_method.get', 'rate', 'TotalGetRequests'),
        ('iis.httpd_request_method.post', 'rate', 'TotalPostRequests'),
        ('iis.httpd_request_method.head', 'rate', 'TotalHeadRequests'),
        ('iis.httpd_request_method.put', 'rate', 'TotalPutRequests'),
        ('iis.httpd_request_method.delete', 'rate', 'TotalDeleteRequests'),
        ('iis.httpd_request_method.options', 'rate', 'TotalOptionsRequests'),
        ('iis.httpd_request_method.trace', 'rate', 'TotalTraceRequests'),

        # Errors
        ('iis.errors.not_found', 'rate', 'TotalNotFoundErrors'),
        ('iis.errors.locked', 'rate', 'TotalLockedErrors'),

        # Users
        ('iis.users.anon', 'rate', 'TotalAnonymousUsers'),
        ('iis.users.nonanon', 'rate', 'TotalNonAnonymousUsers'),

        # Requests
        ('iis.requests.cgi', 'rate', 'TotalCGIRequests'),
        ('iis.requests.isapi', 'rate', 'TotalISAPIExtensionRequests'),
    ]

    def check(self, instance):
        try:
            import wmi
        except ImportError:
            self.log.error("Unable to import 'wmi' module")
            return

        # Connect to the WMI provider
        host = instance.get('host', None)
        user = instance.get('username', None)
        password = instance.get('password', None)
        tags = instance.get('tags', None)
        w = wmi.WMI(host, user=user, password=password)

        try:
            wmi_cls = w.Win32_PerfFormattedData_W3SVC_WebService(name="_Total")
            if not wmi_cls:
                raise Exception('Missing _Total from Win32_PerfFormattedData_W3SVC_WebService')
        except Exception:
            self.log.exception('Unable to fetch Win32_PerfFormattedData_W3SVC_WebService class')
            return

        wmi_cls = wmi_cls[0]
        for metric, mtype, wmi_val in self.METRICS:
            if not hasattr(wmi_cls, wmi_val):
                self.log.error('Unable to fetch metric %s. Missing %s in Win32_PerfFormattedData_W3SVC_WebService' \
                    % (metric, wmi_val))
                continue

            # Submit the metric value with the correct type
            value = getattr(wmi_cls, wmi_val)
            metric_func = getattr(self, mtype)
            metric_func(metric, value, tags=tags)
{'fraction_non_alphanumeric': 0.1355100659786838, 'fraction_numerical': 0.004398578920656403, 'mean_word_length': 2.782469609724888, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3572893', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1810, 'n_words': 355}