Skip to content

Commit 820fdda

Browse files
author
Michael Ro
authored
feat(cassandra): Updates to accept environment variables for Ansible
1 parent 4e40001 commit 820fdda

File tree

2 files changed

+207
-55
lines changed

2 files changed

+207
-55
lines changed

recipes/newrelic/infrastructure/ohi/cassandra/debian.yml

Lines changed: 104 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,6 @@ validationNrql: "SELECT count(*) from CassandraSample SINCE 10 minutes ago WHERE
4747
successLinkConfig:
4848
type: EXPLORER
4949

50-
inputVars:
51-
- name: "NR_CLI_DB_USERNAME"
52-
prompt: "Cassandra connection Username (via JMX)"
53-
- name: "NR_CLI_DB_PASSWORD"
54-
prompt: "Cassandra connection Password (via JMX)"
55-
secret: true
56-
- name: "NR_CLI_DB_HOSTNAME"
57-
prompt: "Cassandra node Hostname (default: localhost)"
58-
default: "localhost"
59-
- name: "NR_CLI_DB_PORT"
60-
prompt: "Cassandra Port (default: 7199)"
61-
default: 7199
62-
6350
preInstall:
6451
info: |2
6552
To capture data from the Cassandra integration, you'll first need to meet these prerequisites:
@@ -74,7 +61,10 @@ install:
7461
default:
7562
cmds:
7663
- task: assert_pre_req
77-
- task: setup
64+
- task: input_assert
65+
vars:
66+
MAX_RETRIES: 3
67+
EXIT131: ""
7868
- task: restart
7969

8070
assert_pre_req:
@@ -86,40 +76,126 @@ install:
8676
exit 1
8777
fi
8878
89-
setup:
90-
label: "Installing Cassandra integration..."
79+
input_assert:
9180
cmds:
9281
- |
82+
TRIES=0
83+
NODETOOL_RESPONSE=""
84+
85+
# Check env vars and set defaults
86+
NEW_RELIC_ASSUME_YES="{{.NEW_RELIC_ASSUME_YES}}"
87+
NEW_RELIC_CASSANDRA_USERNAME=${NEW_RELIC_CASSANDRA_USERNAME:-${NR_CLI_DB_USERNAME:-""}}
88+
NEW_RELIC_CASSANDRA_PASSWORD=${NEW_RELIC_CASSANDRA_PASSWORD:-${NR_CLI_DB_PASSWORD:-""}}
89+
NEW_RELIC_CASSANDRA_HOSTNAME=${NEW_RELIC_CASSANDRA_HOSTNAME:-${NR_CLI_DB_HOSTNAME:-"localhost"}}
90+
NEW_RELIC_CASSANDRA_PORT=${NEW_RELIC_CASSANDRA_PORT:-${NR_CLI_DB_PORT:-7199}}
91+
92+
# Set config path
93+
DEFAULT_CASSANDRA_CONFIG_PATH="/etc/cassandra/cassandra.yaml"
94+
if [ -e "/etc/cassandra/conf/cassandra.yaml" ]; then
95+
DEFAULT_CASSANDRA_CONFIG_PATH="/etc/cassandra/conf/cassandra.yaml"
96+
fi
97+
NEW_RELIC_CASSANDRA_CONFIG_PATH=${NEW_RELIC_CASSANDRA_CONFIG_PATH:-$(echo "$DEFAULT_CASSANDRA_CONFIG_PATH")}
98+
99+
# Interactive mode
100+
if [[ "$NEW_RELIC_ASSUME_YES" != "true" ]]; then
101+
while [ $TRIES -lt {{.MAX_RETRIES}} ]; do
102+
# Check port and hostname
103+
NODETOOL_RESPONSE=$(nodetool -p "$NEW_RELIC_CASSANDRA_PORT" -h "$NEW_RELIC_CASSANDRA_HOSTNAME" version 2>&1)
104+
105+
if [ $(echo $NODETOOL_RESPONSE | grep "Failed to connect" | wc -l) -gt 0 ]; then
106+
printf "\n[Error]: Could not connect to Cassandra with port ${NEW_RELIC_CASSANDRA_PORT} or hostname ${NEW_RELIC_CASSANDRA_HOSTNAME}. Please provide a port number and hostname.\nSee https://docs.newrelic.com/docs/infrastructure/host-integrations/host-integrations-list/cassandra-monitoring-integration/#config-options for more info.\n" >&2
107+
printf "\nCassandra port (default: 7199): "
108+
read -r NEW_RELIC_CASSANDRA_PORT
109+
NEW_RELIC_CASSANDRA_PORT=${NEW_RELIC_CASSANDRA_PORT:-7199}
110+
printf "\nCassandra hostname (default: localhost): "
111+
read -r NEW_RELIC_CASSANDRA_HOSTNAME
112+
NEW_RELIC_CASSANDRA_HOSTNAME=${NEW_RELIC_CASSANDRA_HOSTNAME:-localhost}
113+
((TRIES++))
114+
if [ ! $TRIES -lt {{.MAX_RETRIES}} ]; then exit 131; else continue; fi
115+
fi
116+
117+
# Check if authentication is required
118+
NODETOOL_RESPONSE=$(nodetool -p "$NEW_RELIC_CASSANDRA_PORT" -h "$NEW_RELIC_CASSANDRA_HOSTNAME" version 2>&1)
119+
120+
if [ $(echo $NODETOOL_RESPONSE | grep -i "Credentials required" | wc -l) -gt 0 ]; then
121+
# Check if environment variables have been set and are valid
122+
NODETOOL_RESPONSE=$(nodetool -p "$NEW_RELIC_CASSANDRA_PORT" -h "$NEW_RELIC_CASSANDRA_HOSTNAME" -u "$NEW_RELIC_CASSANDRA_USERNAME" -pw "$NEW_RELIC_CASSANDRA_PASSWORD" version 2>&1)
123+
124+
if [ $(echo $NODETOOL_RESPONSE | grep -Ei "Invalid username or password|Required values.*not provided" | wc -l) -gt 0 ]; then
125+
TRIES=0
126+
printf "\nJMX authentication required"
127+
while [ $TRIES -lt {{.MAX_RETRIES}} ]; do
128+
printf "\nCassandra connection username (via JMX): "
129+
stty -echo
130+
read -r NEW_RELIC_CASSANDRA_USERNAME
131+
stty echo
132+
printf "\n"
133+
printf "Cassandra connection password (via JMX): "
134+
stty -echo
135+
read -r NEW_RELIC_CASSANDRA_PASSWORD
136+
stty echo
137+
printf "\n"
138+
((TRIES++))
139+
140+
NODETOOL_RESPONSE=$(nodetool -p "$NEW_RELIC_CASSANDRA_PORT" -h "$NEW_RELIC_CASSANDRA_HOSTNAME" -u "$NEW_RELIC_CASSANDRA_USERNAME" -pw "$NEW_RELIC_CASSANDRA_PASSWORD" version 2>&1)
141+
if [ $(echo $NODETOOL_RESPONSE | grep "ReleaseVersion" | wc -l) -eq 0 ] ; then
142+
printf "\n[Error]: Could not authenticate on Cassandra server. Check username or password.\nSee https://docs.newrelic.com/docs/infrastructure/host-integrations/host-integrations-list/cassandra-monitoring-integration/#config-options for more info.\n" >&2
143+
if [ ! $TRIES -lt {{.MAX_RETRIES}} ]; then exit 131; else continue; fi
144+
fi
145+
break
146+
done
147+
fi
148+
fi
149+
break
150+
done
151+
fi
152+
153+
# Check for required input in -y mode
154+
if [[ "$NEW_RELIC_ASSUME_YES" == "true" ]]; then
155+
NODETOOL_RESPONSE=$(nodetool -p "$NEW_RELIC_CASSANDRA_PORT" -h "$NEW_RELIC_CASSANDRA_HOSTNAME" -u "$NEW_RELIC_CASSANDRA_USERNAME" -pw "$NEW_RELIC_CASSANDRA_PASSWORD" version 2>&1)
156+
if [ $(echo $NODETOOL_RESPONSE | grep -i "Failed to connect" | wc -l) -gt 0 ]; then
157+
EXIT131=" - NEW_RELIC_CASSANDRA_PORT=<port>\n - NEW_RELIC_CASSANDRA_HOSTNAME=<hostname>"
158+
fi
159+
160+
if [ $(echo $NODETOOL_RESPONSE | grep -Ei "Invalid username or password|Required values.*not provided" | wc -l) -gt 0 ]; then
161+
EXIT131=" - NEW_RELIC_CASSANDRA_USERNAME=<jmx_username>\n - NEW_RELIC_CASSANDRA_PASSWORD=<jmx_user_password>"
162+
fi
163+
fi
164+
165+
if [ "$EXIT131" != "" ]; then
166+
printf "There is a problem with the required environment variables. Please set the following variable(s) and try again:\n\n$EXIT131\n"
167+
exit 131
168+
else
169+
printf "\n[OK] All checks passed. Installing Cassandra Integration...\n\n"
170+
fi
171+
93172
sudo mkdir -p "/etc/newrelic-infra/integrations.d"
94-
- |
173+
95174
# Get latest definitions and skip any failure because of deprecation
96175
sudo apt-get -o Acquire::Check-Valid-Until=false update -yq
97-
- |
98176
sudo apt-get install nri-cassandra -y
99-
- |
177+
100178
if [ -f /etc/newrelic-infra/integrations.d/cassandra-config.yml ]; then
101179
sudo rm /etc/newrelic-infra/integrations.d/cassandra-config.yml;
102180
fi
103181
104182
sudo cp /etc/newrelic-infra/integrations.d/cassandra-config.yml.sample /etc/newrelic-infra/integrations.d/cassandra-config.yml;
105-
106-
- |
107-
sudo tee /etc/newrelic-infra/integrations.d/cassandra-config.yml > /dev/null <<"EOT"
183+
sudo tee /etc/newrelic-infra/integrations.d/cassandra-config.yml > /dev/null << EOT
108184
integrations:
109185
- name: nri-cassandra
110186
env:
111187
METRICS: true
112-
HOSTNAME: {{.NR_CLI_DB_HOSTNAME}}
113-
PORT: {{.NR_CLI_DB_PORT}}
114-
USERNAME: {{.NR_CLI_DB_USERNAME}}
115-
PASSWORD: {{.NR_CLI_DB_PASSWORD}}
188+
HOSTNAME: $NEW_RELIC_CASSANDRA_HOSTNAME
189+
PORT: $NEW_RELIC_CASSANDRA_PORT
190+
USERNAME: $NEW_RELIC_CASSANDRA_USERNAME
191+
PASSWORD: $NEW_RELIC_CASSANDRA_PASSWORD
116192
REMOTE_MONITORING: true
117193
interval: 30s
118194
- name: nri-cassandra
119195
env:
120196
INVENTORY: true
121-
HOSTNAME: {{.NR_CLI_DB_HOSTNAME}}
122-
CONFIG_PATH: /etc/cassandra/cassandra.yaml
197+
HOSTNAME: $NEW_RELIC_CASSANDRA_HOSTNAME
198+
CONFIG_PATH: $NEW_RELIC_CASSANDRA_CONFIG_PATH
123199
REMOTE_MONITORING: true
124200
inventory_source: config/cassandra
125201
interval: 60s

recipes/newrelic/infrastructure/ohi/cassandra/rhel.yml

Lines changed: 103 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,6 @@ validationNrql: "SELECT count(*) from CassandraSample SINCE 10 minutes ago WHERE
5151
successLinkConfig:
5252
type: EXPLORER
5353

54-
inputVars:
55-
- name: "NR_CLI_DB_USERNAME"
56-
prompt: "Cassandra connection Username (via JMX)"
57-
- name: "NR_CLI_DB_PASSWORD"
58-
prompt: "Cassandra connection Password (via JMX)"
59-
secret: true
60-
- name: "NR_CLI_DB_HOSTNAME"
61-
prompt: "Cassandra node Hostname (default: localhost)"
62-
default: "localhost"
63-
- name: "NR_CLI_DB_PORT"
64-
prompt: "Cassandra Port (default: 7199)"
65-
default: 7199
66-
6754
preInstall:
6855
info: |2
6956
To capture data from the Cassandra integration, you'll first need to meet these prerequisites:
@@ -78,7 +65,10 @@ install:
7865
default:
7966
cmds:
8067
- task: assert_pre_req
81-
- task: setup
68+
- task: input_assert
69+
vars:
70+
MAX_RETRIES: 3
71+
EXIT131: ""
8272
- task: restart
8373

8474
assert_pre_req:
@@ -90,39 +80,125 @@ install:
9080
exit 1
9181
fi
9282
93-
setup:
83+
input_assert:
9484
label: "Installing Cassandra integration..."
9585
cmds:
9686
- |
87+
TRIES=0
88+
NODETOOL_RESPONSE=""
89+
90+
# Check env vars and set defaults
91+
NEW_RELIC_ASSUME_YES="{{.NEW_RELIC_ASSUME_YES}}"
92+
NEW_RELIC_CASSANDRA_USERNAME=${NEW_RELIC_CASSANDRA_USERNAME:-${NR_CLI_DB_USERNAME:-""}}
93+
NEW_RELIC_CASSANDRA_PASSWORD=${NEW_RELIC_CASSANDRA_PASSWORD:-${NR_CLI_DB_PASSWORD:-""}}
94+
NEW_RELIC_CASSANDRA_HOSTNAME=${NEW_RELIC_CASSANDRA_HOSTNAME:-${NR_CLI_DB_HOSTNAME:-"localhost"}}
95+
NEW_RELIC_CASSANDRA_PORT=${NEW_RELIC_CASSANDRA_PORT:-${NR_CLI_DB_PORT:-7199}}
96+
97+
# Set config path
98+
DEFAULT_CASSANDRA_CONFIG_PATH="/etc/cassandra/cassandra.yaml"
99+
if [ -e "/etc/cassandra/conf/cassandra.yaml" ]; then
100+
DEFAULT_CASSANDRA_CONFIG_PATH="/etc/cassandra/conf/cassandra.yaml"
101+
fi
102+
NEW_RELIC_CASSANDRA_CONFIG_PATH=${NEW_RELIC_CASSANDRA_CONFIG_PATH:-$(echo "$DEFAULT_CASSANDRA_CONFIG_PATH")}
103+
104+
# Interactive mode
105+
if [[ "$NEW_RELIC_ASSUME_YES" != "true" ]]; then
106+
while [ $TRIES -lt {{.MAX_RETRIES}} ]; do
107+
# Check port and hostname
108+
NODETOOL_RESPONSE=$(nodetool -p "$NEW_RELIC_CASSANDRA_PORT" -h "$NEW_RELIC_CASSANDRA_HOSTNAME" version 2>&1)
109+
110+
if [ $(echo $NODETOOL_RESPONSE | grep "Failed to connect" | wc -l) -gt 0 ]; then
111+
printf "\n[Error]: Could not connect to Cassandra with port ${NEW_RELIC_CASSANDRA_PORT} or hostname ${NEW_RELIC_CASSANDRA_HOSTNAME}. Please provide a port number and hostname.\nSee https://docs.newrelic.com/docs/infrastructure/host-integrations/host-integrations-list/cassandra-monitoring-integration/#config-options for more info.\n" >&2
112+
printf "\nCassandra port (default: 7199): "
113+
read -r NEW_RELIC_CASSANDRA_PORT
114+
NEW_RELIC_CASSANDRA_PORT=${NEW_RELIC_CASSANDRA_PORT:-7199}
115+
printf "\nCassandra hostname (default: localhost): "
116+
read -r NEW_RELIC_CASSANDRA_HOSTNAME
117+
NEW_RELIC_CASSANDRA_HOSTNAME=${NEW_RELIC_CASSANDRA_HOSTNAME:-localhost}
118+
((TRIES++))
119+
if [ ! $TRIES -lt {{.MAX_RETRIES}} ]; then exit 131; else continue; fi
120+
fi
121+
122+
# Check if authentication is required
123+
NODETOOL_RESPONSE=$(nodetool -p "$NEW_RELIC_CASSANDRA_PORT" -h "$NEW_RELIC_CASSANDRA_HOSTNAME" version 2>&1)
124+
125+
if [ $(echo $NODETOOL_RESPONSE | grep -i "Credentials required" | wc -l) -gt 0 ]; then
126+
# Check if environment variables have been set and are valid
127+
NODETOOL_RESPONSE=$(nodetool -p "$NEW_RELIC_CASSANDRA_PORT" -h "$NEW_RELIC_CASSANDRA_HOSTNAME" -u "$NEW_RELIC_CASSANDRA_USERNAME" -pw "$NEW_RELIC_CASSANDRA_PASSWORD" version 2>&1)
128+
129+
if [ $(echo $NODETOOL_RESPONSE | grep -Ei "Invalid username or password|Required values.*not provided" | wc -l) -gt 0 ]; then
130+
TRIES=0
131+
printf "\nJMX authentication required"
132+
while [ $TRIES -lt {{.MAX_RETRIES}} ]; do
133+
printf "\nCassandra connection username (via JMX): "
134+
stty -echo
135+
read -r NEW_RELIC_CASSANDRA_USERNAME
136+
stty echo
137+
printf "\n"
138+
printf "Cassandra connection password (via JMX): "
139+
stty -echo
140+
read -r NEW_RELIC_CASSANDRA_PASSWORD
141+
stty echo
142+
printf "\n"
143+
((TRIES++))
144+
145+
NODETOOL_RESPONSE=$(nodetool -p "$NEW_RELIC_CASSANDRA_PORT" -h "$NEW_RELIC_CASSANDRA_HOSTNAME" -u "$NEW_RELIC_CASSANDRA_USERNAME" -pw "$NEW_RELIC_CASSANDRA_PASSWORD" version 2>&1)
146+
if [ $(echo $NODETOOL_RESPONSE | grep "ReleaseVersion" | wc -l) -eq 0 ] ; then
147+
printf "\n[Error]: Could not authenticate on Cassandra server. Check username or password.\nSee https://docs.newrelic.com/docs/infrastructure/host-integrations/host-integrations-list/cassandra-monitoring-integration/#config-options for more info.\n" >&2
148+
if [ ! $TRIES -lt {{.MAX_RETRIES}} ]; then exit 131; else continue; fi
149+
fi
150+
break
151+
done
152+
fi
153+
fi
154+
break
155+
done
156+
fi
157+
158+
# Check for required input in -y mode
159+
if [[ "$NEW_RELIC_ASSUME_YES" == "true" ]]; then
160+
NODETOOL_RESPONSE=$(nodetool -p "$NEW_RELIC_CASSANDRA_PORT" -h "$NEW_RELIC_CASSANDRA_HOSTNAME" -u "$NEW_RELIC_CASSANDRA_USERNAME" -pw "$NEW_RELIC_CASSANDRA_PASSWORD" version 2>&1)
161+
if [ $(echo $NODETOOL_RESPONSE | grep -i "Failed to connect" | wc -l) -gt 0 ]; then
162+
EXIT131=" - NEW_RELIC_CASSANDRA_PORT=<port>\n - NEW_RELIC_CASSANDRA_HOSTNAME=<hostname>"
163+
fi
164+
165+
if [ $(echo $NODETOOL_RESPONSE | grep -Ei "Invalid username or password|Required values.*not provided" | wc -l) -gt 0 ]; then
166+
EXIT131=" - NEW_RELIC_CASSANDRA_USERNAME=<jmx_username>\n - NEW_RELIC_CASSANDRA_PASSWORD=<jmx_user_password>"
167+
fi
168+
fi
169+
170+
if [ "$EXIT131" != "" ]; then
171+
printf "There is a problem with the required environment variables. Please set the following variable(s) and try again:\n\n$EXIT131\n"
172+
exit 131
173+
else
174+
printf "\n[OK] All checks passed. Installing Cassandra Integration...\n\n"
175+
fi
176+
97177
sudo mkdir -p "/etc/newrelic-infra/integrations.d"
98-
- |
99178
sudo yum -q makecache -y --disablerepo='*' --enablerepo='newrelic-infra'
100-
- |
101179
sudo yum install nri-cassandra -y
102-
- |
180+
103181
if [ -f /etc/newrelic-infra/integrations.d/cassandra-config.yml ]; then
104182
sudo rm /etc/newrelic-infra/integrations.d/cassandra-config.yml;
105183
fi
106184
107185
sudo cp /etc/newrelic-infra/integrations.d/cassandra-config.yml.sample /etc/newrelic-infra/integrations.d/cassandra-config.yml;
108-
109-
- |
110-
sudo tee /etc/newrelic-infra/integrations.d/cassandra-config.yml > /dev/null <<"EOT"
186+
sudo tee /etc/newrelic-infra/integrations.d/cassandra-config.yml > /dev/null << EOT
111187
integrations:
112188
- name: nri-cassandra
113189
env:
114190
METRICS: true
115-
HOSTNAME: {{.NR_CLI_DB_HOSTNAME}}
116-
PORT: {{.NR_CLI_DB_PORT}}
117-
USERNAME: {{.NR_CLI_DB_USERNAME}}
118-
PASSWORD: {{.NR_CLI_DB_PASSWORD}}
191+
HOSTNAME: $NEW_RELIC_CASSANDRA_HOSTNAME
192+
PORT: $NEW_RELIC_CASSANDRA_PORT
193+
USERNAME: $NEW_RELIC_CASSANDRA_USERNAME
194+
PASSWORD: $NEW_RELIC_CASSANDRA_PASSWORD
119195
REMOTE_MONITORING: true
120196
interval: 30s
121197
- name: nri-cassandra
122198
env:
123199
INVENTORY: true
124-
HOSTNAME: {{.NR_CLI_DB_HOSTNAME}}
125-
CONFIG_PATH: /etc/cassandra/conf/cassandra.yaml
200+
HOSTNAME: $NEW_RELIC_CASSANDRA_HOSTNAME
201+
CONFIG_PATH: $NEW_RELIC_CASSANDRA_CONFIG_PATH
126202
REMOTE_MONITORING: true
127203
inventory_source: config/cassandra
128204
interval: 60s

0 commit comments

Comments
 (0)