Your data. Anywhere you go.

New Relic for iOS or Android

Download on the App Store    Android App on Google play

New Relic Insights App for iOS

Download on the App Store

Learn more

Close icon

Relic Solution: How to Set Up Alertable Metrics Using Infrastructure SDK



I recently answered a question from a customer who was trying to set up a custom Infrastructure integration using the Infrastructure SDK, but wanted to make sure that they would be able to set up alert conditions targeting the metrics they would be collecting.

Custom integrations set up using Infrastructure SDK can collect Events, Inventory and Metrics. For setting up alert conditions, it’s easiest to use Metrics. Here is a quick explanation of how you can do this.

Follow the procedures in this document to set up your custom integration. Note that you will need a config file (more on that at this link), a definitions file (more on that at this link) and an executable file. The following is the executable created by a colleague of mine using bash. My custom integration will be tracking the number of files in a directory and the size of that directory as metrics.

Executable file:

# !/bin/sh

# The environment variable DIR_NAME is passed in from dirInfo-config.yaml
# DIR_NAME=`echo "${DIRNAME}"`
  #echo ${DIR_NAME}

# Get the count of files in the directory
# find gets the list, wc counts the lines, tr trims whitespace
FILE_COUNT=`find "${DIR_NAME}" -type f | wc -l | tr -d ' '`

#  echo  "FILE_COUNT is " ${FILE_COUNT}

# Get the directory size, (note that cut breaks off the first field)
DIR_SIZE=`du "${DIR_NAME}" -b -s | cut -f1`
  #echo "DIR_SIZE is " ${DIR_SIZE}

TIME=`date +%s`
  #echo "TIME is " ${TIME}

# Read in the JSON template
# JSON=`cat ./templates/dirInfo-template.json`

# Note that spaces must be encoded as \u0020 in the JSON for events 
JSON="$(cat <<-INSERT
    "metrics": [
    "events": [
            "summary": "directory\u0020was\u0020checked",
            "category": "notifications"

# Replace the values in the JSON
# The @ in the sed command is a delimiter
# s@DIR_NAME@${DIR_NAME} replaces DIR_NAME from the JSON template
JSON=`echo ${JSON} | sed -e "s@DIR_NAME@${DIR_NAME}@g"`
JSON=`echo ${JSON} | sed -e "s@FILE_COUNT@${FILE_COUNT}@g"`
JSON=`echo ${JSON} | sed -e "s@DIR_SIZE@${DIR_SIZE}@g"`
JSON=`echo ${JSON} | sed -e "s@TIME@${TIME}@g"`

# Remove all the whitespace from the JSON
JSON=`echo ${JSON} | tr -d ' \t\n\r\f'`

# Print the result
echo "${JSON}"

In that script, you will notice that I have a section of the JSON defined as "metrics". In that section, there is an "event_type" entry with the value of "JoeSample". I will be using this as my Data Source. Then we have several metrics being collected: fileCount, dirSize, etc. Notice that the numeric metrics (e.g. FILE_COUNT) are not enclosed by quotation marks. If they were enclosed by quotation marks, they would be string values and I wouldn’t be able to alert on them.

Once I have this installed and running, I can find this event and the metrics contained inside by creating an Integrations alert condition from inside the Infrastructure UI (Settings -> Alerts). Here’s what my alert condition looks like, targeting the fileCount metric I just set up:

You will be able to do this using your own custom integration by setting Data Source to the name of your "event_type", then selecting whichever metric you’re interested in that you’ve set up in your custom integration under Define thresholds.

Relic Solution: Setting Up Disk Alerts In Infrastructure

I’ve been looking for a way to move log file existence / not-exist monitors. Thanks for making this the example @Fidelicatessen! Lots of ideas percolating in my head now.