golden hour
/opt/saltstack/salt/lib/python3.10/site-packages/salt/returners
⬆️ Go Up
Upload
File/Folder
Size
Actions
__init__.py
5.98 KB
Del
OK
__pycache__
-
Del
OK
appoptics_return.py
6.11 KB
Del
OK
carbon_return.py
8.24 KB
Del
OK
cassandra_cql_return.py
15.14 KB
Del
OK
cassandra_return.py
2.41 KB
Del
OK
couchbase_return.py
8.88 KB
Del
OK
couchdb_return.py
10.35 KB
Del
OK
django_return.py
2.57 KB
Del
OK
elasticsearch_return.py
12.18 KB
Del
OK
etcd_return.py
7.9 KB
Del
OK
highstate_return.py
15.59 KB
Del
OK
influxdb_return.py
8.03 KB
Del
OK
kafka_return.py
2.12 KB
Del
OK
librato_return.py
4.29 KB
Del
OK
local.py
541 B
Del
OK
local_cache.py
17.39 KB
Del
OK
mattermost_returner.py
4.08 KB
Del
OK
memcache_return.py
5.72 KB
Del
OK
mongo_future_return.py
10.01 KB
Del
OK
mongo_return.py
6.06 KB
Del
OK
multi_returner.py
2.82 KB
Del
OK
mysql.py
18.81 KB
Del
OK
nagios_nrdp_return.py
4.92 KB
Del
OK
odbc.py
7.77 KB
Del
OK
pgjsonb.py
17.36 KB
Del
OK
postgres.py
10.33 KB
Del
OK
postgres_local_cache.py
10.82 KB
Del
OK
pushover_returner.py
6.55 KB
Del
OK
rawfile_json.py
2.23 KB
Del
OK
redis_return.py
8.55 KB
Del
OK
sentry_return.py
5.24 KB
Del
OK
slack_returner.py
6.02 KB
Del
OK
slack_webhook_return.py
11.22 KB
Del
OK
sms_return.py
2.64 KB
Del
OK
smtp_return.py
8.2 KB
Del
OK
splunk.py
5.84 KB
Del
OK
sqlite3_return.py
7.73 KB
Del
OK
syslog_return.py
5.26 KB
Del
OK
telegram_return.py
1.92 KB
Del
OK
xmpp_return.py
4.81 KB
Del
OK
zabbix_return.py
2.45 KB
Del
OK
Edit: mattermost_returner.py
""" Return salt data via mattermost .. versionadded:: 2017.7.0 The following fields can be set in the minion conf file: .. code-block:: yaml mattermost.hook (required) mattermost.username (optional) mattermost.channel (optional) Alternative configuration values can be used by prefacing the configuration. Any values not found in the alternative configuration will be pulled from the default location: .. code-block:: yaml mattermost.channel mattermost.hook mattermost.username mattermost settings may also be configured as: .. code-block:: yaml mattermost: channel: RoomName hook: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx username: user To use the mattermost returner, append '--return mattermost' to the salt command. .. code-block:: bash salt '*' test.ping --return mattermost To override individual configuration items, append --return_kwargs '{'key:': 'value'}' to the salt command. .. code-block:: bash salt '*' test.ping --return mattermost --return_kwargs '{'channel': '#random'}' """ import logging import salt.returners import salt.utils.json import salt.utils.mattermost log = logging.getLogger(__name__) __virtualname__ = "mattermost" def __virtual__(): """ Return virtual name of the module. :return: The virtual name of the module. """ return __virtualname__ def _get_options(ret=None): """ Get the mattermost options from salt. """ attrs = { "channel": "channel", "username": "username", "hook": "hook", "api_url": "api_url", } _options = salt.returners.get_returner_options( __virtualname__, ret, attrs, __salt__=__salt__, __opts__=__opts__ ) log.debug("Options: %s", _options) return _options def returner(ret): """ Send an mattermost message with the data """ _options = _get_options(ret) api_url = _options.get("api_url") channel = _options.get("channel") username = _options.get("username") hook = _options.get("hook") if not hook: log.error("mattermost.hook not defined in salt config") return returns = ret.get("return") message = "id: {}\r\nfunction: {}\r\nfunction args: {}\r\njid: {}\r\nreturn: {}\r\n".format( ret.get("id"), ret.get("fun"), ret.get("fun_args"), ret.get("jid"), returns ) mattermost = post_message(channel, message, username, api_url, hook) return mattermost def event_return(events): """ Send the events to a mattermost room. :param events: List of events :return: Boolean if messages were sent successfully. """ _options = _get_options() api_url = _options.get("api_url") channel = _options.get("channel") username = _options.get("username") hook = _options.get("hook") is_ok = True for event in events: log.debug("Event: %s", event) log.debug("Event data: %s", event["data"]) message = "tag: {}\r\n".format(event["tag"]) for key, value in event["data"].items(): message += "{}: {}\r\n".format(key, value) result = post_message(channel, message, username, api_url, hook) if not result: is_ok = False return is_ok def post_message(channel, message, username, api_url, hook): """ Send a message to a mattermost room. :param channel: The room name. :param message: The message to send to the mattermost room. :param username: Specify who the message is from. :param hook: The mattermost hook, if not specified in the configuration. :return: Boolean if message was sent successfully. """ parameters = dict() if channel: parameters["channel"] = channel if username: parameters["username"] = username parameters["text"] = "```" + message + "```" # pre-formatted, fixed-width text log.debug("Parameters: %s", parameters) result = salt.utils.mattermost.query( api_url=api_url, hook=hook, data="payload={}".format(salt.utils.json.dumps(parameters)), ) log.debug("result %s", result) return bool(result)
Save