Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/collector/otelcol.tmpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
receivers:
{{- if ne .Receivers.HostMetrics nil }}
hostmetrics:
{{- if .Receivers.HostMetrics.CollectionInterval }}
collection_interval: {{ .Receivers.HostMetrics.CollectionInterval }}
{{- end}}
{{- if .Receivers.HostMetrics.InitialDelay }}
initial_delay: {{ .Receivers.HostMetrics.InitialDelay }}
{{- end}}
scrapers:
{{- if .Receivers.HostMetrics.Scrapers }}
{{- if .Receivers.HostMetrics.Scrapers.CPU }}
Expand Down
118 changes: 87 additions & 31 deletions scripts/packages/preinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ set -e
# Description:
# NGINX Agent install script for downloading the NGINX Agent package from the appropriate repository
#
################################
###### Changeable variables
################################

LOG_LEVEL=""

################################
###### Default variables
################################
export NGINX_ONE_HOST="${NGINX_AGENT_SERVER_HOST:-agent.connect.nginx.com}"
export AGENT_GROUP="${AGENT_GROUP:-$(id -ng)}"

RED_COLOUR='\033[0;31m'
NO_COLOUR='\033[0m'

# Determine OS platform
# shellcheck source=/dev/null
. /etc/os-release
Expand Down Expand Up @@ -45,34 +43,92 @@ ensure_sudo() {
}

update_config_file() {
sed_cmd="sed -i.bak "

if [ ! -f "$AGENT_CONFIG_FILE" ]; then
printf "NGINX Agent config file %s does not exist. Could not be updated\n" "$AGENT_CONFIG_FILE"
exit 0
fi
echo "Checking what version of NGINX Agent is already installed"
check_version="nginx-agent --version"
nginx_agent_version=$($check_version 2>&1) || true
if [ -z "${nginx_agent_version##nginx-agent version v2*}" ]; then
echo "Updating NGINX Agent V2 configuration to V3 configuration"
echo "Backing up NGINX Agent V2 configuration to /etc/nginx-agent/nginx-agent-v2-backup.conf"
cp $AGENT_CONFIG_FILE /etc/nginx-agent/nginx-agent-v2-backup.conf

v2_config_file=$AGENT_CONFIG_FILE
v3_config_file=$AGENT_CONFIG_FILE

echo "NGINX Agent server host should be ${NGINX_ONE_HOST}"

if grep -q "$NGINX_ONE_HOST" ${v2_config_file}; then
echo "NGINX Agent is configured to connect to NGINX One"
else
echo "${RED_COLOUR}Previous version of NGINX Agent was not configured to connect to NGINX One. Stopping upgrade${NO_COLOUR}"
exit 1
fi

token=`grep "token:" "${v2_config_file}"`
token=`echo $token | cut -d ":" -f 2 | xargs`

config_dirs=`grep "config_dirs:" "${v2_config_file}"`
config_dirs=`echo $config_dirs | cut -d "\"" -f 2`

allowed_directories=""
IFS=":"
for config_dir in $config_dirs; do
allowed_directories="${allowed_directories}\n - ${config_dir}"
done

allowed_directories="${allowed_directories}\n - /var/log/nginx"

v3_config_contents="
#
# /etc/nginx-agent/nginx-agent.conf
#
# Configuration file for NGINX Agent.
#

if [ "${PACKAGE_HOST}" ]; then
printf "Updating %s ...\n" "${AGENT_CONFIG_FILE}"
log:
# set log level (error, info, debug; default \"info\")
level: info
# set log path. if empty, don't log to file.
path: /var/log/nginx-agent/

# Replace Host
${sed_cmd} "s/host:.*$/host: ${PACKAGE_HOST}/" "${AGENT_CONFIG_FILE}"
fi

# Check the log-level and set accordingly
if [ "${LOG_LEVEL}" ]; then
if [ "$(grep -cP '^(?=[\s]*+[^#])[^#]*(level:)' "${AGENT_CONFIG_FILE}")" -ge 1 ]; then
printf "Setting existing log level: %s\n" "${LOG_LEVEL}"
${sed_cmd} "/^[[:space:]]*#/!s/\(level:.*\)/level: ${LOG_LEVEL}/g" "${AGENT_CONFIG_FILE}"
else
printf "Setting log level: %s\n" "${LOG_LEVEL}"
_log_level_replacement="s/^log:/log:\\
level: ${LOG_LEVEL}/"
allowed_directories: ${allowed_directories}

${sed_cmd} "${_log_level_replacement}" "${AGENT_CONFIG_FILE}"
printf "Successfully updated %s\n" "${AGENT_CONFIG_FILE}"
fi
printf "Successfully updated %s\n" "${AGENT_CONFIG_FILE}"
command:
server:
host: ${NGINX_ONE_HOST}
port: 443
auth:
token: ${token}
tls:
skip_verify: false

collector:
receivers:
host_metrics:
scrapers:
cpu: {}
memory: {}
disk: {}
network: {}
filesystem: {}
processors:
batch: {}
exporters:
otlp_exporters:
- server:
host: ${NGINX_ONE_HOST}
port: 443
authenticator: headers_setter
tls:
skip_verify: false
extensions:
headers_setter:
headers:
- action: insert
key: \"authorization\"
value: ${token}
"

echo "${v3_config_contents}" > $v3_config_file
fi
}

Expand Down
109 changes: 109 additions & 0 deletions scripts/packages/upgrade-agent-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/bin/sh
set -e

# Copyright (C) Nginx, Inc. 2022.
#
# Description:
# NGINX Agent script for converting NGINX AGENT V2 config format to V3 config format

export NGINX_ONE_HOST="${NGINX_AGENT_SERVER_HOST:-agent.connect.nginx.com}"

RED_COLOUR='\033[0;31m'
NO_COLOUR='\033[0m'

for i in "$@"; do
case $i in
--v2-config-file=*)
v2_config_file="${i#*=}"
shift
;;
--v3-config-file=*)
v3_config_file="${i#*=}"
shift
;;
-*|--*)
echo "Unknown option $i"
exit 1
;;
*)
;;
esac
done

echo "NGINX Agent server host should be ${NGINX_ONE_HOST}"

if grep -q "$NGINX_ONE_HOST" ${v2_config_file}; then
echo "NGINX Agent is configured to connect to NGINX One"
else
echo "${RED_COLOUR}Previous version of NGINX Agent was not configured to connect to NGINX One. Stopping upgrade${NO_COLOUR}"
exit 1
fi

token=`grep "token:" "${v2_config_file}"`
token=`echo $token | cut -d ":" -f 2 | xargs`

config_dirs=`grep "config_dirs:" "${v2_config_file}"`
config_dirs=`echo $config_dirs | cut -d "\"" -f 2`

allowed_directories=""
IFS=":"
for config_dir in $config_dirs; do
allowed_directories="${allowed_directories}\n - ${config_dir}"
done

allowed_directories="${allowed_directories}\n - /var/log/nginx"

v3_config_contents="
#
# /etc/nginx-agent/nginx-agent.conf
#
# Configuration file for NGINX Agent.
#

log:
# set log level (error, info, debug; default \"info\")
level: info
# set log path. if empty, don't log to file.
path: /var/log/nginx-agent/

allowed_directories: ${allowed_directories}

command:
server:
host: ${NGINX_ONE_HOST}
port: 443
auth:
token: ${token}
tls:
skip_verify: false

collector:
receivers:
host_metrics:
scrapers:
cpu: {}
memory: {}
disk: {}
network: {}
filesystem: {}
processors:
batch: {}
exporters:
otlp_exporters:
- server:
host: ${NGINX_ONE_HOST}
port: 443
authenticator: headers_setter
tls:
skip_verify: false
extensions:
headers_setter:
headers:
- action: insert
key: \"authorization\"
value: ${token}
"

echo "${v3_config_contents}" > $v3_config_file

echo "NGINX Agent configuration successfully upgraded"
1 change: 1 addition & 0 deletions test/docker/nginx-official-image/apk/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ WORKDIR /agent
COPY ./build/${PACKAGE_NAME}.apk /agent/build/${PACKAGE_NAME}.apk
COPY ./ /agent

RUN apk del nginx-agent
RUN apk add --allow-untrusted /agent/build/${PACKAGE_NAME}.apk

RUN unlink /var/log/nginx/access.log
Expand Down
3 changes: 2 additions & 1 deletion test/docker/nginx-official-image/deb/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ WORKDIR /agent
COPY ./build/${PACKAGE_NAME}.deb /agent/build/${PACKAGE_NAME}.deb
COPY ./ /agent

RUN apt-get update \
RUN apt-get update \
&& apt purge -y nginx-agent \
&& apt install --no-install-recommends --no-install-suggests -y /agent/build/${PACKAGE_NAME}.deb \
&& rm /agent/build/${PACKAGE_NAME}.deb

Expand Down