Working with New Relic (newrelic infra) and Terraform in Unix/Linux

New Relic is a web application, aimed primarily at developers. Its main objective is to track the ways users interact with designed products. This tool analysts.

Key features of New Relic:

  • Monitoring of the availability.
  • Warning and notification.
  • Performance profiles.
  • Plugins.
  • Histograms and percentiles.
  • The API access.
  • Response time in real-time.
  • The performance analyzer of the Java virtual machine.
  • Error detection, analysis.
  • The database call response time and throughput.
  • Diagnostics, transaction tracing and the stack trace.
  • Analysis of response time, throughput and breakdown by components.
  • Details on SQL performance.
  • Analysis of the user in real-time when browsing web pages.
  • Custom dashbord.
  • Track individual business transactions.
  • X-Ray session for business transactions.
  • Cross-application tracing for distributed applications.
  • Reports on availability, scalability, deployment.

Install terraform in Unix/Linux

The installation is very primitive and I described how to do this here:

Install terraform in Unix/Linux

So, in this article, I created a script to automatically install the software. It has been tested on CentOS 6/7, Debian 8, and on Mac OS X. Everything is working properly!

To get help on commands, run:

$ terraform --help
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
 apply Builds or changes infrastructure
 console Interactive console for Terraform interpolations
 destroy Destroy Terraform-managed infrastructure
 env Workspace management
 fmt Rewrites config files to canonical format
 get Download and install modules for the configuration
 graph Create a visual graph of Terraform resources
 import Import existing infrastructure into Terraform
 init Initialize a Terraform working directory
 output Read an output from a state file
 plan Generate and show an execution plan
 providers Prints a tree of the providers used in the configuration
 push Upload this Terraform module to Atlas to run
 refresh Update local state file against real resources
 show Inspect Terraform state or plan
 taint Manually mark a resource for recreation
 untaint Manually unmark a resource as tainted
 validate Validates the Terraform files
 version Prints the Terraform version
 workspace management Workspace

All other commands:
 debug Debug output management (experimental)
 force-unlock Manually unlock the terraform state
 state Advanced state management

Start using!

Working with New Relic (newrelic infra) and Terraform in Unix/Linux

I have the terraform folder, in it I will lay providers with whom I work. Because in this example I will use newrelic, it will create the folder and move into it. Further, in this folder, you should create:

$ mkdir examples modules

In the examples folder, I keep so called “playbook” to razvarivaya various services, for example — zabbix-server, grafana, web servers, and so on. In modules directory I will store all the necessary modules.

Can start writing the module, but for this task, I will create a folder:

$ mkdir modules/newrelic_infra

Go to it:

$ cd modules/newrelic_infra

Open the file:

$ vim newrelic_infra_alert_condition.tf

In this file, insert:

#---------------------------------------------------
# Add newrelic infra alert condition
#---------------------------------------------------
resource "newrelic_infra_alert_condition" "infra_alert_condition" {
 count = "${var.infra_alert_condition && !var.infra_alert_condition_with_warning ? 1 : 0}"

 policy_id = "${var.infra_alert_condition_policy_id}"
 name = "${var.infra_alert_condition_name !="" ? "${lower(var.infra_alert_condition_name)}" : "${var.name}-infra-alert-condition-${lower(var.environment)}" }"
 type = "${var.infra_alert_condition_type}"
 event = "${var.infra_alert_condition_event}"
 select = "${var.infra_alert_condition_select}"
 comparison = "${var.infra_alert_condition_comparison}"

 enabled = "${var.infra_alert_condition_enabled}"
 where = "${var.infra_alert_condition_where}"
 process_where = "${var.infra_alert_condition_process_where}"
 integration_provider = "${var.infra_alert_condition_integration_provider}"

 critical {
 duration = "${var.infra_alert_condition_critical_duration}"
 value = "${var.infra_alert_condition_critical_value}"
 time_function = "${var.infra_alert_condition_critical_time_function}"
}

 lifecycle = {
 create_before_destroy = true,
 ignore_changes = []
}

 depends_on = []
}

#---------------------------------------------------
# Add newrelic infra alert condition with warning
#---------------------------------------------------
resource "newrelic_infra_alert_condition" "infra_alert_condition_with_warning" {
 count = "${var.infra_alert_condition && var.infra_alert_condition_with_warning ? 1 : 0}"

 policy_id = "${var.infra_alert_condition_policy_id}"
 name = "${var.infra_alert_condition_name !="" ? "${lower(var.infra_alert_condition_name)}" : "${var.name}-infra-alert-condition-${lower(var.environment)}" }"
 type = "${var.infra_alert_condition_type}"
 event = "${var.infra_alert_condition_event}"
 select = "${var.infra_alert_condition_select}"
 comparison = "${var.infra_alert_condition_comparison}"

 enabled = "${var.infra_alert_condition_enabled}"
 where = "${var.infra_alert_condition_where}"
 process_where = "${var.infra_alert_condition_process_where}"
 integration_provider = "${var.infra_alert_condition_integration_provider}"

 critical {
 duration = "${var.infra_alert_condition_critical_duration}"
 value = "${var.infra_alert_condition_critical_value}"
 time_function = "${var.infra_alert_condition_critical_time_function}"
}

 warning {
 duration = "${var.infra_alert_condition_warning_duration}"
 value = "${var.infra_alert_condition_warning_value}"
 time_function = "${var.infra_alert_condition_warning_time_function}"
}

 lifecycle = {
 create_before_destroy = true,
 ignore_changes = []
}

 depends_on = []
}

Open the file:

$ vim variables.tf

And write:

#-----------------------------------------------------------
# Global 
#-----------------------------------------------------------
variable "name" {
 description = "The name for newrelic_alert resources"
 default = "test"
} 

variable "environment" {
 description = "environment"
 default = "prod"
}

#-----------------------------------------------------------
# newrelic_infra_alert_condition
#-----------------------------------------------------------
variable "infra_alert_condition" {
 description = "Enable newrelic_infra_alert_condition"
 default = "false"
}

variable "infra_alert_condition_policy_id" {
 description = "(Required) The ID of the alert policy where this condition should be used."
 default = ""
}

variable "infra_alert_condition_name" {
 description = "(Required) The Infrastructure alert condition''s name."
 default = ""
}

variable "infra_alert_condition_type" {
 description = "(Required) The type of Infrastructure alert condition: 'infra_process_running', 'infra_metric', or 'infra_host_not_reporting'."
 default = "infra_metric"
}

variable "infra_alert_condition_event" {
 description = "(Required) The metric event; for example, system metrics, process metrics, storage metrics or network metrics."
 default = ""
}

variable "infra_alert_condition_select" {
 description = "(Required) The name attribute to identify the type of metric condition; for example, 'network', 'process', 'system', or 'storage'."
 default = "system"
}

variable "infra_alert_condition_comparison" {
 description = "(Required) The operator used to evaluate the threshold value; 'above', 'below', 'equal'."
 default = "equal"
}

variable "infra_alert_condition_enabled" {
 description = "(Optional) Set whether to enable the alert condition. Defaults to true."
 default = "true"
}

variable "infra_alert_condition_where" {
 description = "(Optional) Infrastructure host filter for the alert condition."
 default = ""
}

variable "infra_alert_condition_process_where" {
 description = "(Optional) Any filters applied to processes; for example: commandName = 'java'."
 default = ""
}

variable "infra_alert_condition_integration_provider" {
 description = "(Optional) For alerts on integrations, use this instead of event."
 default = ""
}


variable "infra_alert_condition_critical_duration" {
 description = "(Required) Identifies the number of minutes the threshold must be met or passed for the alert to trigger. Threshold durations must be between 1 and 60 minutes (inclusive)."
 default = 1
}

variable "infra_alert_condition_critical_value" {
 description = "(Optional) Threshold value, computed against the comparison operator. Supported by 'infra_metric' and 'infra_process_running' alert condition types."
 default = 1
}

variable "infra_alert_condition_critical_time_function" {
 description = "(Optional) Indicates if the condition needs to be sustained or to just break the threshold once; all or any. Supported by the 'infra_metric' alert condition type."
 default = "all"
}

#-----------------------------------------------------------
# newrelic_infra_alert_condition with warning
#-----------------------------------------------------------
variable "infra_alert_condition_with_warning" {
 description = "Enable newrelic_infra_alert_condition with warning usage"
 default = "false"
}

variable "infra_alert_condition_warning_duration" {
 description = "(Required) Identifies the number of minutes the threshold must be met or passed for the alert to trigger. Threshold durations must be between 1 and 60 minutes (inclusive)."
 default = 1
}

variable "infra_alert_condition_warning_value" {
 description = "(Optional) Threshold value, computed against the comparison operator. Supported by 'infra_metric' and 'infra_process_running' alert condition types."
 default = 1
}

variable "infra_alert_condition_warning_time_function" {
 description = "(Optional) Indicates if the condition needs to be sustained or to just break the threshold once; all or any. Supported by the 'infra_metric' alert condition type."
 default = "all"
}

Actually in this file are stored all the variables. Thanks cap!

Open the last file:

$ vim outputs.tf

And need to insert the following lines:

#-----------------------------------------------------------
# newrelic_infra_alert_condition
#-----------------------------------------------------------
output "infra_alert_condition_id" {
 description = "ID for newrelic_nrql_alert_condition"
 value = "${newrelic_infra_alert_condition.infra_alert_condition.*.id}"
}

output "infra_alert_condition__with_warning_id" {
 description = "ID for newrelic_nrql_alert_condition"
 value = "${newrelic_infra_alert_condition.infra_alert_condition_with_warning.*.id}"
}

Now go to the folder aws/examples and create another folder to check is written of a miracle:

$ mkdir newrelic_infra && cd $_

Inside the folders you created open the file:

$ vim main.tf

And paste the following code into it:

#
# MAINTAINER: Vitaliy Natarov "[email protected]"
#
terraform {
 required_version = "~> 0.11.11"
}

provider "newrelic" {
 api_key = "75e6741e6326cce1666ecfb94c3c0b8fdf"
}

module "newrelic_alert" {
 source = "../../modules/newrelic_alert"

 # vars for newrelic_alert_policy
 alert_policy = "true"
 #alert_policy_name = "new-relic-policy-PER_CONDITION"
 alert_policy_incident_preference = "PER_CONDITION"
 alert_policy_simple_default = "true"

 alert_channel = "true"
#
 alert_channel_slack = "true"
 alert_channel_slack_configuration_channel = "new-relic"
 alert_channel_slack_configuration_url = "https://hooks.slack.com/services/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666"
#
 alert_condition = "true"
 alert_condition_policy_id = "${element(module.newrelic_alert.alert_policy_id, 0)}"
 #alert_condition_policy_id = "${element(module.newrelic_alert.simple_default_alert_policy_id, 0)}"
 alert_condition_type = "apm_app_metric"
 alert_condition_entities = ["PHP Application"]

 alert_policy_channel = "false"
}

module "newrelic_infra" {
 source = "../../modules/newrelic_infra"

 infra_alert_condition = "true"
 infra_alert_condition_policy_id = "${element(module.newrelic_alert.alert_policy_id, 0)}"
 infra_alert_condition_type = "infra_metric"
 infra_alert_condition_event = "StorageSample"
 infra_alert_condition_select = "diskUsedPercent"
 infra_alert_condition_comparison = "equal"
 infra_alert_condition_where = "(`hostname` LIKE '%frontend%')"
 infra_alert_condition_integration_provider = ""

#
 infra_alert_condition_with_warning = "true" 
} 

In the file is to register all needed, but most importantly:

  • api_key — the API key which you can generate in “‘Account settings’->’API keys'”.

Everything is already written and ready to use. Well, let’s start testing. In the folder with your playboycom performed:

$ terraform init

This action, I initialize the project. Then, tighten the module:

$ terraform get

PS: To update the changes in the module itself, you can run:

$ terraform get-update

Check the validation:

$ terraform validate

Zapuskaem run:

$ terraform plan

I brought everything I have is good and you can run the deployment:

$ terraform apply

As you can see from the output, everything went smoothly! To remove creation, you can run:

$ terraform destroy

Another utility:

Working with New Relic (newrelic alert) and Terraform in Unix/Linux

Working with New Relic (newrelic nrql) and Terraform in Unix/Linux

All material Uploader in github account for easy use:

$ git clone https://github.com/SebastianUA/terraform.git

But that’s about it. This article “Working with New Relic (newrelic infra) and Terraform in Unix/Linux” is completed.

Source: linux-notes.org

(Visited 97 times, 1 visits today)