<commit_before>#!/usr/bin/env python
import argparse
import datetime
import json
import logging
import re
import subprocess
import time


__version__ = '1.1.2'


logger = logging.getLogger(__name__)


r_recv = re.compile(' ([0-9]+) packets received')
r_wrong = re.compile(' ([0-9]+) packets to unknown port received')
r_miss = re.compile(' ([0-9]+) packet receive errors')
r_sent = re.compile(' ([0-9]+) packets sent')


def get_args():
    desc = 'Report UDP packet statistics to stdout or CloudWatch.'
    parser = argparse.ArgumentParser(description=desc)
    parser.add_argument('--region', help='AWS region (default: us-east-1)',
                        type=str, default='us-east-1')
    parser.add_argument('--loop', action='store_true')
    parser.add_argument('--to-cloudwatch', action='store_true')
    parser.add_argument('--to-stdout', action='store_true')
    parser.add_argument('--cw-asg', action='store_true',
                        help='report with Auto Scaling Group dimension')
    parser.add_argument('--show-rates', action='store_true')
    parser.add_argument('--rate-sample-time', type=float, default=20.,
                        help=('Either the loop interval (with --loop) or '
                              'cron interval if running from cron '
                              '(in seconds)'))
    parser.add_argument('--stats-file', type=str, required=True)
    parser.add_argument('--loglevel', type=str, default='WARN')
    parser.add_argument('--log-file', type=str, required=False)
    return parser.parse_args()


def gen_stats():
    ns = subprocess.check_output(['netstat', '-su'])
    return {
        'received': int(r_recv.search(ns).groups(0)[0]),
        'wrong': int(r_wrong.search(ns).groups(0)[0]),
        'missed': int(r_miss.search(ns).groups(0)[0]),
        'sent': int(r_sent.search(ns).groups(0)[0]),
        'dt': datetime.datetime.now(),
    }

<commit_msg>fix: sometimes netstat returns 1<commit_after>#!/usr/bin/env python
import argparse
import datetime
import json
import logging
import re
import subprocess
import time


__version__ = '1.1.2'


logger = logging.getLogger(__name__)


r_recv = re.compile(' ([0-9]+) packets received')
r_wrong = re.compile(' ([0-9]+) packets to unknown port received')
r_miss = re.compile(' ([0-9]+) packet receive errors')
r_sent = re.compile(' ([0-9]+) packets sent')


def get_args():
    desc = 'Report UDP packet statistics to stdout or CloudWatch.'
    parser = argparse.ArgumentParser(description=desc)
    parser.add_argument('--region', help='AWS region (default: us-east-1)',
                        type=str, default='us-east-1')
    parser.add_argument('--loop', action='store_true')
    parser.add_argument('--to-cloudwatch', action='store_true')
    parser.add_argument('--to-stdout', action='store_true')
    parser.add_argument('--cw-asg', action='store_true',
                        help='report with Auto Scaling Group dimension')
    parser.add_argument('--show-rates', action='store_true')
    parser.add_argument('--rate-sample-time', type=float, default=20.,
                        help=('Either the loop interval (with --loop) or '
                              'cron interval if running from cron '
                              '(in seconds)'))
    parser.add_argument('--stats-file', type=str, required=True)
    parser.add_argument('--loglevel', type=str, default='WARN')
    parser.add_argument('--log-file', type=str, required=False)
    return parser.parse_args()


def gen_stats():
    ns = subprocess.check_output('netstat -su || true', shell=True)
    return {
        'received': int(r_recv.search(ns).groups(0)[0]),
        'wrong': int(r_wrong.search(ns).groups(0)[0]),
        'missed': int(r_miss.search(ns).groups(0)[0]),
        'sent': int(r_sent.search(ns).groups(0)[0]),
        'dt': datetime.datetime.now(),
    }


{'fraction_non_alphanumeric': 0.16363636363636364, 'fraction_numerical': 0.012207792207792207, 'mean_word_length': 3.1587473002159827, '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': '2727405', 'n_tokens_mistral': 1225, 'n_tokens_neox': 1176, 'n_words': 256}