File: //bigscoots/wpo/extras/otel.sh
#!/bin/bash
### package check ###
source /bigscoots/wpo/extras/bigscoots.menu
if ! yum list installed at --quiet &>/dev/null; then
echo "Installing packages"
yum install -y at --quiet
fi
#### Check if compose is installed, if not install it ###
if ! composer -v --no-interaction &>/dev/null; then
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
mv composer.phar /usr/local/bin/composer
fi
### Select Domain ###
DOMAINS=$(find "/home/nginx/domains/" -maxdepth 1 -type d -exec test -d "{}/log" \; -print | sed 's#.*/##' | sort)
if [ -z "$DOMAINS" ]; then
echo "No domains found in $DOMAIN_DIR"
exit 1
fi
### menu ###
PS3="Select a domain: "
select domain in $DOMAINS; do
if [ -n "$domain" ]; then
echo "You selected: $domain"
selected_domain="$domain"
break
else
echo "Invalid option. Please select a valid domain."
fi
done
### PHP vers check ###
PHPOTEL=$(grep otel /usr/local/nginx/conf/conf.d/$domain.ssl.conf | head -n 1 | cut -d/ -f 6 | cut -d_ -f 1)
PHPVER=$(grep php /usr/local/nginx/conf/conf.d/$domain.ssl.conf | grep remi | head -n 1 | cut -d/ -f 6 | sed 's/php\([0-9]*\)-remi\.conf;/\1/')
if [[ "$1" == "remove" ]]; then
UNIX_SOCK=$(grep fastcgi_pass /usr/local/nginx/conf/otel_$domain.conf | grep -v '\#' | awk '{print $2}' | cut -d: -f2 | tr -d ';')
echo "Revert version"
if [[ "$UNIX_SOCK" = "14000" ]]; then
sed -i "s|otel_$domain.conf|php80-remi.conf|g" /usr/local/nginx/conf/conf.d/$domain.ssl.conf
elif [[ "$UNIX_SOCK" = "16000" ]]; then
sed -i "s|otel_$domain.conf|php81-remi.conf|g" /usr/local/nginx/conf/conf.d/$domain.ssl.conf
elif [[ "$UNIX_SOCK" = "18000" ]]; then
sed -i "s|otel_$domain.conf|php82-remi.conf|g" /usr/local/nginx/conf/conf.d/$domain.ssl.conf
fi
rm -f /usr/local/nginx/conf/otel_$domain.conf
exit 1
fi
if [[ -z "$PHPVER" && "$PHPOTEL" == "otel" ]]; then
echo "Otel is already enabled, skipping"
exit 1
elif [[ -z "$PHPVER" ]]; then
echo "Unsupported PHP version, please upgrade PHP >= 8.0.0"
exit 1
elif [[ -n "$PHPVER" && "$PHPVER" == "74" ]]; then
echo "Unsupported PHP version, please upgrade PHP >= 8.0.0"
exit 1
else
echo "$domain is using REMI php $PHPVER version"
OTEL=$(/opt/remi/php$PHPVER/root/usr/bin/php -i | grep opentelemetry)
fi
### Check if selected version has otel installed if not install it ###
if [[ -z "$OTEL" && -n "$PHPVER" ]]; then
if [[ ! -e "/opt/remi/php$PHPVER/root/usr/bin/pecl" ]]; then
wget http://pear.php.net/go-pear.phar -P /tmp
/opt/remi/php$PHPVER/root/usr/bin/php /tmp/go-pear.phar
fi
export CFLAGS="-std=c99"
echo "Installing opentelemetry library"
/opt/remi/php$PHPVER/root/usr/bin/pecl -q install opentelemetry >/dev/null 2>&1
unset CFLAGS
echo "Installing protobuf library"
/opt/remi/php$PHPVER/root/usr/bin/pecl -q install protobuf-3.19.4 >/dev/null 2>&1
rm -f /tmp/go-pear.phar
fi
### Update php.ini and run composer install
if [ ! -d "/etc/opt/remi/php$PHPVER/otel/" ]; then
mkdir /etc/opt/remi/php$PHPVER/otel
cat > /etc/opt/remi/php$PHPVER/otel/composer.json <<EOF
{
"name": "opentelemetry/wordpress",
"description": "Instrumentation for Opentelemetry",
"type": "project",
"minimum-stability": "beta",
"require": {
"open-telemetry/opentelemetry-auto-wordpress": "^0.0.15",
"open-telemetry/sdk": "^1.0@beta",
"open-telemetry/exporter-otlp": "^1.0@beta",
"php-http/guzzle7-adapter": "^1.0"
},
"config": {
"allow-plugins": {
"php-http/discovery": true
}
}
}
EOF
echo "Running composer install"
/opt/remi/php$PHPVER/root/usr/bin/php /usr/local/bin/composer install --ignore-platform-reqs --working-dir=/etc/opt/remi/php$PHPVER/otel
echo "Update php.ini"
cat > /etc/opt/remi/php$PHPVER/php.d/50-opentelemetry.ini <<EOF
auto_prepend_file=/etc/opt/remi/php$PHPVER/otel/vendor/autoload.php
[opentelemetry]
extension=opentelemetry.so
extension=protobuf.so
EOF
fi
### Generating configuration file ###
echo "Enter the duration in minutes, for how long you want to enable metrics:"
read at_time
cp /usr/local/nginx/conf/php$PHPVER-remi.conf /usr/local/nginx/conf/otel_$domain.conf
content_to_add="fastcgi_param OTEL_PHP_AUTOLOAD_ENABLED true;\nfastcgi_param OTEL_SERVICE_NAME $domain;\nfastcgi_param OTEL_EXPORTER_OTLP_ENDPOINT http://38.65.227.26:4318;\nfastcgi_param OTEL_TRACES_EXPORTER otlp;\nfastcgi_param OTEL_METRICS_EXPORTER otlp;\nfastcgi_param OTEL_LOGS_EXPORTER otlp;"
sed -i "91i\\$content_to_add" /usr/local/nginx/conf/otel_$domain.conf
### Updating vhost and basedir###
sed -i "s|php$PHPVER-remi.conf|otel_$domain.conf|g" /usr/local/nginx/conf/conf.d/$domain.ssl.conf
BASE_CHECK=$(grep open_basedir /etc/centminmod/php.d/bsdomains.ini | grep $domain)
BASE_PHP=$(echo "$BASE_CHECK" | grep -oP '/etc/opt/remi/php\K\d+' | head -1)
if [[ ! "$BASE_CHECK" == *otel* ]]; then
sed -i "s|$BASE_CHECK|$BASE_CHECK:/etc/opt/remi/php$PHPVER/otel|g" /etc/centminmod/php.d/bsdomains.ini
elif [[ "$BASE_CHECK" == *otel* && "$BASE_PHP" != "$PHPVER" ]]; then
sed -i "s|$BASE_PHP|$PHPVER|g" /etc/centminmod/php.d/bsdomains.ini
fi
### Scheduler ###
echo "sed -i 's|otel_$domain.conf|php$PHPVER-remi.conf|g' /usr/local/nginx/conf/conf.d/$domain.ssl.conf; rm -f /usr/local/nginx/conf/otel_$domain.conf; scoots php restart $PHPVER" | at now + $at_time minute
### restart ###
nprestart; scoots php restart $PHPVER