<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time
import socket


class PortMon(object):
    '''
    Config should be in /etc/sd-agent/config.cfg like:
    
    portmon_timeout: 5
    portmon_targets: 127.0.0.1:22,192.168.3.4:5678
    
    Multiple targets to be monitored should be separated with a ','.
    '''
    
    def __init__(self, agentConfig, checksLogger, rawConfig):
        self.agentConfig    = agentConfig
        self.checksLogger   = checksLogger
        self.rawConfig      = rawConfig
        
        self.timeout    = int( rawConfig['Main'].get('portmon_timeout', 10) )
        targets_str     = rawConfig['Main'].get('portmon_targets', '').strip()
        if len(targets_str):
            self.targets = targets_str.split(',')
        else:
            self.targets = []
        
    
    def run(self):
        target_times = {}
        for t in self.targets:
            host, port = t.split(':')
            target_times[t.replace('.','_')] = self.timeConnect(host, int(port))
        return target_times
        
    
    def timeConnect(self, host, port):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(self.timeout)
        t_init = time.time()
        #ret = None
        try:
            s.connect((host, port))
            ret = time.time() - t_init
            #s.recv(0)
            s.close()
        except (socket.error, socket.timeout):
            return None
        return ret
        <commit_msg>Return string 'down' if could not connect<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time
import socket


class PortMon(object):
    '''
    Config should be in /etc/sd-agent/config.cfg like:
    
    portmon_timeout: 5
    portmon_targets: 127.0.0.1:22,192.168.3.4:5678
    
    Multiple targets to be monitored should be separated with a ','.
    '''
    
    def __init__(self, agentConfig, checksLogger, rawConfig):
        self.agentConfig    = agentConfig
        self.checksLogger   = checksLogger
        self.rawConfig      = rawConfig
        
        self.timeout    = int( rawConfig['Main'].get('portmon_timeout', 10) )
        targets_str     = rawConfig['Main'].get('portmon_targets', '').strip()
        if len(targets_str):
            self.targets = targets_str.split(',')
        else:
            self.targets = []
        
    
    def run(self):
        target_times = {}
        for t in self.targets:
            host, port = t.split(':')
            target_times[t.replace('.','_')] = self.timeConnect(host, int(port))
        return target_times
        
    
    def timeConnect(self, host, port):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(self.timeout)
        t_init = time.time()
        #ret = None
        try:
            s.connect((host, port))
            ret = time.time() - t_init
            #s.recv(0)
            s.close()
        except (socket.error, socket.timeout):
            return 'down'
        return ret
        
{'fraction_non_alphanumeric': 0.12433862433862433, 'fraction_numerical': 0.016534391534391533, 'mean_word_length': 1.845719661335842, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '463667', 'n_tokens_mistral': 959, 'n_tokens_neox': 906, 'n_words': 198}