From 5543232fbe95e02e8f3d182428c14bbca023b2ec Mon Sep 17 00:00:00 2001 From: Dimitris Gkanatsios Date: Wed, 28 Feb 2024 15:37:04 -0800 Subject: [PATCH] updates --- .../PF_StartupScript.ps1 | 9 +++++++ .../PF_StartupScript.sh | 26 +++++++++++++++++++ .../README.md | 17 +++++++----- .../telegraf.conf | 7 ++--- 4 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 windows_linux_custom_gameserver_metrics/PF_StartupScript.sh diff --git a/windows_linux_custom_gameserver_metrics/PF_StartupScript.ps1 b/windows_linux_custom_gameserver_metrics/PF_StartupScript.ps1 index 99c1d54..02ebe90 100644 --- a/windows_linux_custom_gameserver_metrics/PF_StartupScript.ps1 +++ b/windows_linux_custom_gameserver_metrics/PF_StartupScript.ps1 @@ -24,3 +24,12 @@ echo "PlayFab Virtual Machine ID is $env:PF_VM_ID" # e.g. vmss:SouthCentralUs:24 echo "Region where the VM is deployed is $env:PF_REGION" # e.g. SouthCentralUs echo "Shared content folder is $env:PF_SHARED_CONTENT_FOLDER_VM" # e.g. D:\sharedcontentfolder (All servers running on this VM have access to this folder through the PF_SHARED_CONTENT_FOLDER env variable.) +# install telegraf as a Windows service +$telegrafPath = 'C:\Program Files\telegraf' +New-Item -ItemType Directory -Force -Path "$telegrafPath" +cp "$scriptPath\telegraf.*" "$telegrafPath" +cd "$telegrafPath" +.\telegraf.exe --service install --config "$telegrafPath\telegraf.conf" + +# start the telegraf service +.\telegraf.exe --service start \ No newline at end of file diff --git a/windows_linux_custom_gameserver_metrics/PF_StartupScript.sh b/windows_linux_custom_gameserver_metrics/PF_StartupScript.sh new file mode 100644 index 0000000..88e532d --- /dev/null +++ b/windows_linux_custom_gameserver_metrics/PF_StartupScript.sh @@ -0,0 +1,26 @@ +#!/bin/bash +set -o errexit # script exits when a command fails == set -e +set -o nounset # script exits when tries to use undeclared variables == set -u +set -o xtrace # trace what's executed == set -x (useful for debugging) +set -o pipefail # causes pipelines to retain / set the last non-zero status + +# get script's path +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + +# install telegraf +sudo apt-get -yq --no-install-recommends install "${DIR}"/telegraf.deb + +systemctl stop telegraf + +# remove \r\n in-place +sed -i -e 's/\r$//' ${DIR}/telegraf.conf + +mkdir -p /etc/telegraf || echo 'Warning: /etc/telegraf already exists' + +# add PF_TITLE_ID, PF_BUILD_ID, PF_VM_ID to telegraf.conf as dimensions +sed -e "s/_%PF_TITLE_ID%_/${PF_TITLE_ID}/g" -e "s/_%PF_BUILD_ID%_/${PF_BUILD_ID}/g" -e "s/_%PF_VM_ID%_/${PF_VM_ID}/g" -e "s/_%PF_REGION%_/${PF_REGION}/g" ${DIR}/telegraf.conf > /etc/telegraf/telegraf.conf + +# add user telegraf to group docker so telegraf daemon can read docker logs +sudo usermod -a -G docker telegraf + +systemctl restart telegraf diff --git a/windows_linux_custom_gameserver_metrics/README.md b/windows_linux_custom_gameserver_metrics/README.md index a810db2..6fb58c2 100644 --- a/windows_linux_custom_gameserver_metrics/README.md +++ b/windows_linux_custom_gameserver_metrics/README.md @@ -6,11 +6,11 @@ ## What it does -This script uses [telegraf](https://www.influxdata.com/time-series-platform/telegraf/) agent that sets up an HTTP server to listen for external metrics. Using this solution, you can send any kind of metrics from your game server to telegraf. You can use any of the [telegraf output plugins](https://github.com/influxdata/telegraf/blob/master/docs/OUTPUTS.md) to store them. +This script uses [telegraf](https://www.influxdata.com/time-series-platform/telegraf/) agent that sets up an HTTP server to listen for external metrics. Using this solution, you can send any kind of metrics from your game server to telegraf and you can use any of the [telegraf output plugins](https://github.com/influxdata/telegraf/blob/master/docs/OUTPUTS.md) to store them, in the backend of your choice. -The documentation for the [http listener telegraf input plugin is here](https://github.com/influxdata/telegraf/blob/master/plugins/inputs/http_listener_v2/README.md). +The documentation for the [http listener telegraf input plugin is here](https://github.com/influxdata/telegraf/blob/master/plugins/inputs/http_listener_v2/README.md), check it out for instructions on how to properly configure it. You can also check this [telegraf.conf sample config file](./telegraf.conf) file for an example of how to configure telegraf to listen for metrics on port 8080. -You can see the [telegraf.conf](./telegraf.conf) file for an example of how to configure telegraf to listen for metrics on port 8080. To send metrics from your game server, you need to find the localhost IP address and send a POST request to the telegraf server. +To send custom metrics, you need send a POST request to the telegraf server from your game server. To do that, you need to find the host VM IP address. This is different depending on the type of Build you are using. ### Host IP using Windows or Linux Processes @@ -18,7 +18,7 @@ If you are using [process mode](https://learn.microsoft.com/en-us/gaming/playfab ### Host IP using Windows Containers -To get the IP of the host when using Windows Containers, you can use the following Powershell script from inside the container, before you start your game server: +PlayFab configures a bridge network between containers and the host VM. To get the IP of the host when using Windows Containers, you can use the following Powershell script from inside the container, before you start your game server: ```powershell $gateway = (Get-WmiObject -Class Win32_IP4RouteTable | where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0' } | sort metric1 | select nexthop).nexthop @@ -27,12 +27,16 @@ Write-Output $gateway ### Host IP using Linux Containers -To get the IP of the host when using Linux Containers, you can a script from inside the container, before you start your game server. The script you car run depends on your container image. If, for example, you are using Ubuntu, you can do `sudo apt update && sudo apt install net-tools` and then grab the gateway IP using: +PlayFab configures a bridge network between containers and the host VM. To get the IP of the host when using Linux Containers, you can run a script from inside the container, before you start your game server. The script you can run depends on the OS that you configure your container image with. For example, if you are using Ubuntu, you can do `sudo apt update && sudo apt install net-tools` to install `netstat` and then grab the host VM (which acts as a gateway) IP using: ```bash netstat -rn | grep '^0.0.0.0' | awk '{print $2}' ``` +## Sending metrics to telegraf + +To send metrics to telegraf, you'd need to send the data in a specific format. You can see the documentation [here](https://github.com/influxdata/telegraf/blob/master/plugins/inputs/http_listener_v2/README.md) and a couple of samples below: + ```bash curl -i -XPOST 'http://localhost:8080/telegraf' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000' ``` @@ -47,12 +51,11 @@ $response = Invoke-WebRequest -Uri "http://localhost:8080/telegraf" -Method POST ## Usage -You should download telegraf from the [GitHub releases](https://github.com/influxdata/telegraf/releases) section. The [Windows amd64 package](https://dl.influxdata.com/telegraf/releases/telegraf-1.24.4_windows_amd64.zip) works with this sample. Then, you should grab either the DeveloperSecretKey or the TelemetryKey from your PlayFab account and add it to the plugin.conf file. +You should download telegraf from the [GitHub releases](https://github.com/influxdata/telegraf/releases) section. The [Windows amd64 package](https://dl.influxdata.com/telegraf/releases/telegraf-1.24.4_windows_amd64.zip) works with this sample. Then, create a zip file with the following contents: - telegraf.conf -- plugin.conf - telegraf package file (e.g. telegraf.exe) - PF_StartupScript.ps1 file diff --git a/windows_linux_custom_gameserver_metrics/telegraf.conf b/windows_linux_custom_gameserver_metrics/telegraf.conf index cbcdb93..fef4b75 100644 --- a/windows_linux_custom_gameserver_metrics/telegraf.conf +++ b/windows_linux_custom_gameserver_metrics/telegraf.conf @@ -11,12 +11,13 @@ omit_hostname = true [global_tags] - titleID = "59F84" - buildID = "f49eeebd-9a7a-4bd1-a7a9-92b5b1a9568d" - vmID = "vmss:EastUs:59F84_a7582365-e048-4a88-9271-40a74b87ba83:45c93cbe-83d0-4220-9f25-24b5e4f20665" + titleID = "_%PF_TITLE_ID%_" + buildID = "_%PF_BUILD_ID%_" + vmID = "_%PF_VM_ID%_" [[outputs.file]] ## Files to write to, "stdout" is a specially handled file. + # this is just for testing, you should add the outputs of your choice files = ["stdout"] # Generic HTTP write listener