| #!/bin/bash |
| # |
| # Script to install Cloud Monitoring and Logging Agent on lab host. |
| # |
| # Assumptions: |
| # The Cloud Project service account key file ($HOME/luci/swarming-key.json) |
| # is available. This is used to upload metrics and logs to the Cloud Project. |
| # |
| # Prerequisites: |
| # All companion python modules are copied to same directory. |
| |
| set -e |
| |
| function print_help { |
| echo -e " |
| Usage: $0 -n <hostname> -f <fqdn> -p <project_id> -z <zone_name> |
| Available options: |
| \t-n\thostname |
| \t-f\tfully qualified domain name |
| \t-p\tproject id |
| \t-z\tzone name |
| \t-?,-h\tHelp |
| " |
| } |
| |
| hostname="uninitialized" |
| fqdn="uninitialized" |
| ip="uninitialized" |
| project_id="uninitialized" |
| zone_name="uninitialized" |
| |
| while getopts "?h:n:f:i:p:z:" opt |
| do |
| case $opt in |
| n) hostname=$OPTARG;; |
| f) fqdn=$OPTARG;; |
| i) ip=$OPTARG;; |
| p) project_id=$OPTARG;; |
| z) zone_name=$OPTARG;; |
| ?|h) print_help; exit 1;; |
| esac |
| done |
| shift $((OPTIND-1)) |
| |
| root_dir="$( cd "$(dirname "$0")" ; pwd -P )" |
| config_transformer="$root_dir/generate_monitoring_configs.py" |
| |
| # Install jinja2 |
| pip install jinja2 |
| |
| # Set up GCP service account credential |
| echo "Setting up GCP credentials - started" |
| APPLICATION_DEFAULT_CREDS="/etc/google/auth/application_default_credentials.json" |
| sudo mkdir -p /etc/google/auth |
| sudo cp "$HOME/luci/swarming-key.json" "$APPLICATION_DEFAULT_CREDS" |
| sudo chown root:root "$APPLICATION_DEFAULT_CREDS" |
| sudo chmod 0400 "$APPLICATION_DEFAULT_CREDS" |
| echo "Setting up GCP credentials - success" |
| |
| # Install Monitoring Agent |
| echo "Installing monitoring agent - started" |
| cd $HOME |
| curl -sSO https://dl.google.com/cloudagents/install-monitoring-agent.sh |
| sudo env REPO_SUFFIX=unstable bash install-monitoring-agent.sh |
| echo "installing monitoring agent - success" |
| |
| # Install Logging Agent |
| echo "Installing logging agent - started" |
| cd $HOME |
| curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh |
| sudo env REPO_SUFFIX=unstable bash install-logging-agent.sh |
| echo "Installing logging agent - success" |
| |
| /usr/bin/python $config_transformer --debug --hostname $hostname --fqdn $fqdn --ip $ip --project_id $project_id --zone_name $zone_name |
| sudo cp $root_dir/collectd.conf /etc/stackdriver/collectd.conf |
| sudo cp $root_dir/google-fluentd.conf /etc/google-fluentd/google-fluentd.conf |
| sudo service stackdriver-agent restart |
| sudo service google-fluentd restart |