def define_and_get_from_env( self, environment_value, default_value=None, convert_to=None, required_field=None, max_value=None, min_value=None, env_name=None, ): """ Tests the entire process of defining a configuration option that can be set via the environment, setting the environment variable, and retrieving its value from an empty MonitorConfig object. :param environment_value: The value to set in the environment before trying to retrieve it. If None, no environment value will be set. :param default_value: The default value to use when defining the option. :param convert_to: The convert_to value to use when defining the option. :param required_field: The required_field value to use when defining the option. :param max_value: The max_value value to use when defining the option. :param min_value: The max_value value to use when defining the option. :param env_name: The env_name value to use when defining the option. :return: The value retrieved from the option :type environment_value: six.text_type|None :type convert_to: ArrayOfStrings|JsonArray|six.text_type|Number|bool :type default_value: six.text_type :type required_field: bool :type max_value: Number :type min_value: Number :type env_name: six.text_type :rtype: ArrayOfStrings|JsonArray|six.text_type|Number|bool """ define_config_option( "foo_env", "foo", "Some description", default=default_value, convert_to=convert_to, required_option=required_field, max_value=max_value, min_value=min_value, env_aware=True, env_name=env_name, ) if env_name is None: env_name = "SCALYR_FOO" if environment_value is not None: os_environ_unicode[env_name] = environment_value else: os_environ_unicode.pop(env_name, None) try: return MonitorConfig(monitor_module="foo_env").get("foo") finally: os_environ_unicode.pop(env_name, None)
def test_list_of_strings(self): define_config_option('foo', 'some_param', 'A list of strings', default=['a', 'b', 'c', 'd']) self.assertEquals(self.get(['x', 'y', 'z'], convert_to=None), ['x', 'y', 'z']) self.assertRaises(Exception, lambda: self.get("['x', 'y', 'z']", convert_to=list), ['x', 'y', 'z'])
def test_list_of_strings(self): define_config_option( "foo", "some_param", "A list of strings", default=["a", "b", "c", "d"] ) self.assertEquals(self.get(["x", "y", "z"], convert_to=None), ["x", "y", "z"]) self.assertRaises( Exception, lambda: self.get("['x', 'y', 'z']", convert_to=list), ["x", "y", "z"], )
def test_define_config_option(self): define_config_option("foo", "a", "Description", required_option=True, convert_to=int) self.assertRaises(BadMonitorConfiguration, MonitorConfig, {"b": 1}, monitor_module="foo") config = MonitorConfig({"a": "5"}, monitor_module="foo") self.assertEquals(config.get("a"), 5) define_config_option( "foo", "b", "Description", min_value=5, max_value=10, default=7, convert_to=int, ) config = MonitorConfig({"a": 5}, monitor_module="foo") self.assertEquals(config.get("b"), 7) self.assertRaises( BadMonitorConfiguration, MonitorConfig, { "a": 5, "b": 1 }, monitor_module="foo", ) self.assertRaises( BadMonitorConfiguration, MonitorConfig, { "a": 5, "b": 11 }, monitor_module="foo", ) # Test case where no value in config for option with no default value should result in no value in # MonitorConfig object define_config_option("foo", "c", "Description", min_value=5, max_value=10, convert_to=int) config = MonitorConfig({"a": 5}, monitor_module="foo") self.assertTrue("c" not in config)
def test_define_config_option(self): define_config_option('foo', 'a', 'Description', required_option=True, convert_to=int) self.assertRaises(BadMonitorConfiguration, MonitorConfig, {'b': 1}, monitor_module='foo') config = MonitorConfig({'a': '5'}, monitor_module='foo') self.assertEquals(config.get('a'), 5) define_config_option('foo', 'b', 'Description', min_value=5, max_value=10, default=7, convert_to=int) config = MonitorConfig({'a': 5}, monitor_module='foo') self.assertEquals(config.get('b'), 7) self.assertRaises(BadMonitorConfiguration, MonitorConfig, { 'a': 5, 'b': 1 }, monitor_module='foo') self.assertRaises(BadMonitorConfiguration, MonitorConfig, { 'a': 5, 'b': 11 }, monitor_module='foo') # Test case where no value in config for option with no default value should result in no value in # MonitorConfig object define_config_option('foo', 'c', 'Description', min_value=5, max_value=10, convert_to=int) config = MonitorConfig({'a': 5}, monitor_module='foo') self.assertTrue('c' not in config)
def test_define_config_option(self): define_config_option('foo', 'a', 'Description', required_option=True, convert_to=int) self.assertRaises(BadMonitorConfiguration, MonitorConfig, {'b': 1}, monitor_module='foo') config = MonitorConfig({'a': '5'}, monitor_module='foo') self.assertEquals(config.get('a'), 5) define_config_option('foo', 'b', 'Description', min_value=5, max_value=10, default=7, convert_to=int) config = MonitorConfig({'a': 5}, monitor_module='foo') self.assertEquals(config.get('b'), 7) self.assertRaises(BadMonitorConfiguration, MonitorConfig, {'a': 5, 'b': 1}, monitor_module='foo') self.assertRaises(BadMonitorConfiguration, MonitorConfig, {'a': 5, 'b': 11}, monitor_module='foo') # Test case where no value in config for option with no default value should result in no value in # MonitorConfig object define_config_option('foo', 'c', 'Description', min_value=5, max_value=10, convert_to=int) config = MonitorConfig({'a': 5}, monitor_module='foo') self.assertTrue('c' not in config)
# Note, this can be run in standalone mode by: # python -m scalyr_agent.run_monitor # scalyr_agent.builtin_monitors.nginx_monitor import httplib import urllib2 import socket import urlparse from scalyr_agent.scalyr_monitor import ScalyrMonitor, define_metric, define_log_field, define_config_option httpSourceAddress = "127.0.0.1" __monitor__ = __name__ define_config_option(__monitor__, 'module', 'Always ``scalyr_agent.builtin_monitors.nginx_monitor``', convert_to=str, required_option=True) define_config_option(__monitor__, 'status_url', 'Optional (defaults to \'http://localhost/nginx_status\'). The URL the monitor will fetch' 'to retrieve the nginx status information.', default='http://localhost/nginx_status') define_config_option(__monitor__, 'source_address', 'Optional (defaults to \'%s\'). The IP address to be used as the source address when fetching ' 'the status URL. Many servers require this to be 127.0.0.1 because they only server the status ' 'page to requests from localhost.' % httpSourceAddress, default=httpSourceAddress) define_config_option(__monitor__, 'id', 'Optional (defaults to empty string). Included in each log message generated by this monitor, ' 'as a field named ``instance``. Allows you to distinguish between different nginx instances ' 'running on the same server.', convert_to=str) define_log_field(__monitor__, 'monitor', 'Always ``nginx_monitor``.') define_log_field(__monitor__, 'metric', 'The metric name. See the metric tables for more information.')
from scalyr_agent.scalyr_monitor import ( ScalyrMonitor, define_metric, define_log_field, define_config_option, ) httpSourceAddress = "127.0.0.1" __monitor__ = __name__ define_config_option( __monitor__, "module", "Always ``scalyr_agent.builtin_monitors.nginx_monitor``", convert_to=str, required_option=True, ) define_config_option( __monitor__, "status_url", "Optional (defaults to 'http://localhost/nginx_status'). The URL the monitor will fetch" "to retrieve the nginx status information.", default="http://localhost/nginx_status", ) define_config_option( __monitor__, "source_address", "Optional (defaults to '%s'). The IP address to be used as the source address when fetching " "the status URL. Many servers require this to be 127.0.0.1 because they only server the status "
# python -m scalyr_agent.run_monitor # scalyr_agent.builtin_monitors.nginx_monitor import httplib import urllib2 import socket import urlparse from scalyr_agent.scalyr_monitor import ScalyrMonitor, define_metric, define_log_field, define_config_option httpSourceAddress = "127.0.0.1" __monitor__ = __name__ define_config_option(__monitor__, 'module', 'Always ``scalyr_agent.builtin_monitors.nginx_monitor``', convert_to=str, required_option=True) define_config_option( __monitor__, 'status_url', 'Optional (defaults to \'http://localhost/nginx_status\'). The URL the monitor will fetch' 'to retrieve the nginx status information.', default='http://localhost/nginx_status') define_config_option( __monitor__, 'source_address', 'Optional (defaults to \'%s\'). The IP address to be used as the source address when fetching ' 'the status URL. Many servers require this to be 127.0.0.1 because they only server the status ' 'page to requests from localhost.' % httpSourceAddress, default=httpSourceAddress)
def test_list_of_strings(self): define_config_option('foo', 'some_param', 'A list of strings', default=['a', 'b', 'c', 'd']) self.assertEquals(self.get(['x', 'y', 'z'], convert_to=None), ['x', 'y', 'z']) self.assertRaises(Exception, lambda: self.get("['x', 'y', 'z']", convert_to=list), ['x', 'y', 'z'])