|
6 | 6 | # Description: |
7 | 7 | # NGINX Agent install script for downloading the NGINX Agent package from the appropriate repository |
8 | 8 | # |
9 | | -################################ |
10 | | -###### Changeable variables |
11 | | -################################ |
12 | | - |
13 | | -LOG_LEVEL="" |
14 | | - |
15 | 9 | ################################ |
16 | 10 | ###### Default variables |
17 | 11 | ################################ |
| 12 | +export NGINX_ONE_HOST="${NGINX_AGENT_SERVER_HOST:-agent.connect.nginx.com}" |
18 | 13 | export AGENT_GROUP="${AGENT_GROUP:-$(id -ng)}" |
19 | 14 |
|
| 15 | +RED_COLOUR='\033[0;31m' |
| 16 | +NO_COLOUR='\033[0m' |
| 17 | + |
20 | 18 | # Determine OS platform |
21 | 19 | # shellcheck source=/dev/null |
22 | 20 | . /etc/os-release |
@@ -45,34 +43,92 @@ ensure_sudo() { |
45 | 43 | } |
46 | 44 |
|
47 | 45 | update_config_file() { |
48 | | - sed_cmd="sed -i.bak " |
49 | | - |
50 | | - if [ ! -f "$AGENT_CONFIG_FILE" ]; then |
51 | | - printf "NGINX Agent config file %s does not exist. Could not be updated\n" "$AGENT_CONFIG_FILE" |
52 | | - exit 0 |
53 | | - fi |
| 46 | + echo "Checking what version of NGINX Agent is already installed" |
| 47 | + check_version="nginx-agent --version" |
| 48 | + nginx_agent_version=$($check_version 2>&1) || true |
| 49 | + if [ -z "${nginx_agent_version##nginx-agent version v2*}" ]; then |
| 50 | + echo "Updating NGINX Agent V2 configuration to V3 configuration" |
| 51 | + echo "Backing up NGINX Agent V2 configuration to /etc/nginx-agent/nginx-agent-v2-backup.conf" |
| 52 | + cp $AGENT_CONFIG_FILE /etc/nginx-agent/nginx-agent-v2-backup.conf |
| 53 | + |
| 54 | + v2_config_file=$AGENT_CONFIG_FILE |
| 55 | + v3_config_file=$AGENT_CONFIG_FILE |
| 56 | + |
| 57 | + echo "NGINX Agent server host should be ${NGINX_ONE_HOST}" |
| 58 | + |
| 59 | + if grep -q "$NGINX_ONE_HOST" ${v2_config_file}; then |
| 60 | + echo "NGINX Agent is configured to connect to NGINX One" |
| 61 | + else |
| 62 | + echo "${RED_COLOUR}Previous version of NGINX Agent was not configured to connect to NGINX One. Stopping upgrade${NO_COLOUR}" |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | + |
| 66 | + token=`grep "token:" "${v2_config_file}"` |
| 67 | + token=`echo $token | cut -d ":" -f 2 | xargs` |
| 68 | + |
| 69 | + config_dirs=`grep "config_dirs:" "${v2_config_file}"` |
| 70 | + config_dirs=`echo $config_dirs | cut -d "\"" -f 2` |
| 71 | + |
| 72 | + allowed_directories="" |
| 73 | + IFS=":" |
| 74 | + for config_dir in $config_dirs; do |
| 75 | + allowed_directories="${allowed_directories}\n - ${config_dir}" |
| 76 | + done |
| 77 | + |
| 78 | + allowed_directories="${allowed_directories}\n - /var/log/nginx" |
| 79 | + |
| 80 | + v3_config_contents=" |
| 81 | +# |
| 82 | +# /etc/nginx-agent/nginx-agent.conf |
| 83 | +# |
| 84 | +# Configuration file for NGINX Agent. |
| 85 | +# |
54 | 86 |
|
55 | | - if [ "${PACKAGE_HOST}" ]; then |
56 | | - printf "Updating %s ...\n" "${AGENT_CONFIG_FILE}" |
| 87 | +log: |
| 88 | + # set log level (error, info, debug; default \"info\") |
| 89 | + level: info |
| 90 | + # set log path. if empty, don't log to file. |
| 91 | + path: /var/log/nginx-agent/ |
57 | 92 |
|
58 | | - # Replace Host |
59 | | - ${sed_cmd} "s/host:.*$/host: ${PACKAGE_HOST}/" "${AGENT_CONFIG_FILE}" |
60 | | - fi |
61 | | - |
62 | | - # Check the log-level and set accordingly |
63 | | - if [ "${LOG_LEVEL}" ]; then |
64 | | - if [ "$(grep -cP '^(?=[\s]*+[^#])[^#]*(level:)' "${AGENT_CONFIG_FILE}")" -ge 1 ]; then |
65 | | - printf "Setting existing log level: %s\n" "${LOG_LEVEL}" |
66 | | - ${sed_cmd} "/^[[:space:]]*#/!s/\(level:.*\)/level: ${LOG_LEVEL}/g" "${AGENT_CONFIG_FILE}" |
67 | | - else |
68 | | - printf "Setting log level: %s\n" "${LOG_LEVEL}" |
69 | | - _log_level_replacement="s/^log:/log:\\ |
70 | | - level: ${LOG_LEVEL}/" |
| 93 | +allowed_directories: ${allowed_directories} |
71 | 94 |
|
72 | | - ${sed_cmd} "${_log_level_replacement}" "${AGENT_CONFIG_FILE}" |
73 | | - printf "Successfully updated %s\n" "${AGENT_CONFIG_FILE}" |
74 | | - fi |
75 | | - printf "Successfully updated %s\n" "${AGENT_CONFIG_FILE}" |
| 95 | +command: |
| 96 | + server: |
| 97 | + host: ${NGINX_ONE_HOST} |
| 98 | + port: 443 |
| 99 | + auth: |
| 100 | + token: ${token} |
| 101 | + tls: |
| 102 | + skip_verify: false |
| 103 | + |
| 104 | +collector: |
| 105 | + receivers: |
| 106 | + host_metrics: |
| 107 | + scrapers: |
| 108 | + cpu: {} |
| 109 | + memory: {} |
| 110 | + disk: {} |
| 111 | + network: {} |
| 112 | + filesystem: {} |
| 113 | + processors: |
| 114 | + batch: {} |
| 115 | + exporters: |
| 116 | + otlp_exporters: |
| 117 | + - server: |
| 118 | + host: ${NGINX_ONE_HOST} |
| 119 | + port: 443 |
| 120 | + authenticator: headers_setter |
| 121 | + tls: |
| 122 | + skip_verify: false |
| 123 | + extensions: |
| 124 | + headers_setter: |
| 125 | + headers: |
| 126 | + - action: insert |
| 127 | + key: \"authorization\" |
| 128 | + value: ${token} |
| 129 | + " |
| 130 | + |
| 131 | + echo "${v3_config_contents}" > $v3_config_file |
76 | 132 | fi |
77 | 133 | } |
78 | 134 |
|
|
0 commit comments