Skip to content

Commit

Permalink
Collectors restructure: all collectors moved to available dir
Browse files Browse the repository at this point in the history
  • Loading branch information
vasiliyk committed May 26, 2024
1 parent 4040d84 commit 881e215
Show file tree
Hide file tree
Showing 60 changed files with 325 additions and 270 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 5 additions & 3 deletions collectors/etc/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# This file is part of tcollector.
# Copyright (C) 2010 The tcollector Authors.
# Copyright (C) 2010-2024 The tcollector Authors.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -30,22 +30,24 @@
import os
import sys


def onload(options, tags):
"""Function called by tcollector when it starts up.
Args:
options: The options as returned by the OptionParser.
tags: A dictionnary that maps tag names to tag values.
tags: A dictionary that maps tag names to tag values.
"""
pass


def get_defaults():
"""Configuration values to use as defaults in the code
This is called by the OptionParser.
"""

default_cdir = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'collectors')
default_cdir = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'collectors/enabled')

defaults = {
'verbose': False,
Expand Down
43 changes: 43 additions & 0 deletions collectors/test/linux/procstats_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python
# This file is part of tcollector.
# Copyright (C) 2014 The tcollector Authors.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version. This program is distributed in the hope that it
# will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
# General Public License for more details. You should have received a copy
# of the GNU Lesser General Public License along with this program. If not,
# see <http://www.gnu.org/licenses/>.

import signal
import subprocess
import time
import unittest
import os


class ProcstatsTests(unittest.TestCase):
"""Just make sure you can run a collector without it blowing up."""

def test_start_terminate(self):
env = os.environ.copy()
if env.get("PYTHONPATH"):
env["PYTHONPATH"] += ":."
else:
env["PYTHONPATH"] = "."
p = subprocess.Popen(["collectors/available/linux/long-lived/procstats.py"], env=env,
stdout=subprocess.PIPE)
time.sleep(5)
p.terminate()
time.sleep(1)
if p.poll() is None:
p.kill()
self.assertEqual(p.poll(), -signal.SIGTERM)
self.assertIn(b"proc.", p.stdout.read())


if __name__ == '__main__':
unittest.main()
237 changes: 237 additions & 0 deletions collectors/test/linux/udp_bridge_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
#!/usr/bin/env python
# This file is part of tcollector.
# Copyright (C) 2014 The tcollector Authors.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version. This program is distributed in the hope that it
# will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
# General Public License for more details. You should have received a copy
# of the GNU Lesser General Public License along with this program. If not,
# see <http://www.gnu.org/licenses/>.

import unittest
import tcollector
import sys
import mocks


def return_none(x):
return None


def always_true():
return True


class UDPCollectorTests(unittest.TestCase):

def setUp(self):
if 'udp_bridge.py' not in tcollector.COLLECTORS: # pylint: disable=maybe-no-member
raise unittest.SkipTest("udp_bridge unavailable")

self.saved_exit = sys.exit
self.saved_stderr = sys.stderr
self.saved_stdout = sys.stdout
self.udp_bridge = tcollector.COLLECTORS['udp_bridge.py'] # pylint: disable=maybe-no-member
self.udp_globals = {}

sys.exit = return_none
bridge_file = open(self.udp_bridge.filename)
try:
exec(compile(bridge_file.read(), self.udp_bridge.filename, 'exec'), self.udp_globals)
finally:
bridge_file.close()
sys.exit = self.saved_exit

self.udp_globals['socket'] = mocks.Socket()
self.udp_globals['sys'] = mocks.Sys()
self.udp_globals['udp_bridge_conf'].enabled = always_true
self.udp_globals['utils'] = mocks.Utils()

def run_bridge_test(self, udpInputLines, stdoutLines, stderrLines):
mockSocket = self.udp_globals['socket'] = mocks.Socket()
mockSocket.state['udp_in'] = list(udpInputLines)

self.udp_globals['sys'] = mocks.Sys()
self.udp_globals['sys'].stderr.lines = stderrLines
self.udp_globals['sys'].stdout.lines = stdoutLines
sys.stderr = self.udp_globals['sys'].stderr
sys.stdout = self.udp_globals['sys'].stdout

try:
self.udp_globals['main']()
except mocks.SocketDone:
pass
finally:
sys.stderr = self.saved_stderr
sys.stdout = self.saved_stdout

def test_populated(self):
# assertIsInstance, assertIn, assertIsNone do not exist in Python 2.6
self.assertTrue(isinstance(self.udp_bridge, tcollector.Collector),
msg="self.udp_bridge not instance of tcollector.Collector") # pylint: disable=maybe-no-member
self.assertEqual(self.udp_bridge.proc, None)
self.assertTrue('main' in self.udp_globals, msg="'main' not in self.udp_globals")

def test_single_line_no_put(self):
inputLines = [
'foo.bar 1 1'
]
expected = '\n'.join(inputLines) + '\n'
stderr = []
stdout = []
self.run_bridge_test(inputLines, stdout, stderr)

self.assertEqual(''.join(stdout), expected)
self.assertEqual(stderr, [])

def test_single_line_put(self):
inputLines = [
'put foo.bar 1 1'
]
expected = '\n'.join([
'foo.bar 1 1'
]) + '\n'
stderr = []
stdout = []

self.run_bridge_test(inputLines, stdout, stderr)
self.assertEqual(''.join(stdout), expected)
self.assertEqual(stderr, [])

def test_multi_line_no_put(self):
inputLines = [
'foo.bar 1 1',
'bar.baz 2 2'
]
expected = '\n'.join(inputLines) + '\n'
stderr = []
stdout = []

self.run_bridge_test(inputLines, stdout, stderr)
self.assertEqual(''.join(stdout), expected)
self.assertEqual(stderr, [])

def test_multi_line_put(self):
inputLines = [
'put foo.bar 1 1',
'put bar.baz 2 2'
]
expected = '\n'.join([
'foo.bar 1 1',
'bar.baz 2 2'
]) + '\n'
stderr = []
stdout = []

self.run_bridge_test(inputLines, stdout, stderr)
self.assertEqual(''.join(stdout), expected)
self.assertEqual(stderr, [])

def test_multi_line_mixed_put(self):
inputLines = [
'put foo.bar 1 1',
'bar.baz 2 2',
'put foo.bar 3 3'
]
expected = '\n'.join([
'foo.bar 1 1',
'bar.baz 2 2',
'foo.bar 3 3'
]) + '\n'
stderr = []
stdout = []

self.run_bridge_test(inputLines, stdout, stderr)
self.assertEqual(''.join(stdout), expected)
self.assertEqual(stderr, [])

def test_multi_line_no_put_cond(self):
inputLines = [
'foo.bar 1 1\nbar.baz 2 2'
]
expected = '\n'.join(inputLines) + '\n'
stderr = []
stdout = []

self.run_bridge_test(inputLines, stdout, stderr)
self.assertEqual(''.join(stdout), expected)
self.assertEqual(stderr, [])

def test_multi_line_put_cond(self):
inputLines = [
'put foo.bar 1 1\nput bar.baz 2 2'
]
expected = '\n'.join([
'foo.bar 1 1',
'bar.baz 2 2'
]) + '\n'
stderr = []
stdout = []

self.run_bridge_test(inputLines, stdout, stderr)
self.assertEqual(''.join(stdout), expected)
self.assertEqual(stderr, [])

def test_multi_empty_line_no_put(self):
inputLines = [
'foo.bar 1 1',
'',
'bar.baz 2 2'
]
expected = 'foo.bar 1 1\n'
stderr = []
stdout = []

self.run_bridge_test(inputLines, stdout, stderr)
self.assertEqual(''.join(stdout), expected)
self.assertEqual(stderr, ['invalid data\n'])

def test_multi_empty_line_put(self):
inputLines = [
'put foo.bar 1 1',
'',
'put bar.baz 2 2'
]
expected = 'foo.bar 1 1\n'
stderr = []
stdout = []

self.run_bridge_test(inputLines, stdout, stderr)
self.assertEqual(''.join(stdout), expected)
self.assertEqual(stderr, ['invalid data\n'])

def test_multi_empty_line_no_put_cond(self):
inputLines = [
'foo.bar 1 1\n\nbar.baz 2 2'
]
expected = '\n'.join(inputLines) + '\n'
stderr = []
stdout = []

self.run_bridge_test(inputLines, stdout, stderr)
self.assertEqual(''.join(stdout), expected)
self.assertEqual(stderr, [])

def test_multi_empty_line_put_cond(self):
inputLines = [
'put foo.bar 1 1\n\nput bar.baz 2 2'
]
expected = '\n'.join([
'foo.bar 1 1',
'',
'bar.baz 2 2'
]) + '\n'
stderr = []
stdout = []

self.run_bridge_test(inputLines, stdout, stderr)
self.assertEqual(''.join(stdout), expected)
self.assertEqual(stderr, [])


if __name__ == '__main__':
unittest.main()
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 881e215

Please sign in to comment.