Python
This is a plugin library to enable logging to Rapid7 Insight from the Python Logger. Additionally this plugin allows the user to get an overview of methods being executed, their execution time, as well as CPU and Memory statistics. Note that this plugin is asynchronous.
Install the library
The library can be installed using the following pip command.
pip install r7insight_python
You can also place logentries in your requirements.txt file
Insert your Logging Code
Use the following snippet as an example usage of the library.
#!/usr/bin/env python
import logging
from r7insight import R7InsightHandler
log = logging.getLogger('r7insight')
log.setLevel(logging.INFO)
test = R7InsightHandler(TOKEN, REGION)
log.addHandler(test)
log.warn("Warning message")
log.info("Info message")
sleep(10)
Please note that this is an asynchronous appender.
The parameter you need to fill in is TOKEN which is the token we copied earlier. It connects this logger with the file on InsightOps. If you haven’t taken note of the token when creating your log, you can retrieve it by logging in to InsightOps. Once logged in, browse to the log you created, click the “settings” icon and the token is available
By default the appender will attempt to send it’s logs over SSL over port 443
import time
import logging
from r7insight import R7InsightHandler, metrics
TEST = metrics.Metric(METRIC_TOKEN, REGION)
@TEST.metric()
def function_one(t):
"""A dummy function that takes some time."""
time.sleep(t)
if __name__ == '__main__':
function_one(1)
This will result in the following output.
Tue Aug 18 09:44:37 IST 2015 : INFO, function_name=function_one execution_time=1.00419783592 cpu=4.0 cpu_count=4 memory=svmem(total=8589934592L, available=4230602752L, percent=50.7, used=5016002560L, free=3571707904L, active=3454431232L, inactive=658894848L, wired=902676480L)