### Run Example "Hello, World" Container Source: https://app.scalyr.com/help/install-agent-docker A simple Docker command to run a container that continuously echoes 'Hello, World!'. This is used to demonstrate logging setup with the Scalyr Agent. ```shell docker run -d ubuntu /bin/sh -c 'while true; do echo Hello, World!; sleep 1; done' ``` -------------------------------- ### Run Example "Hello, World" Container (Syslog Driver) Source: https://app.scalyr.com/help/install-agent-docker Starts a 'Hello, World!' container configured to send its logs via the Syslog logging driver to the Scalyr Agent running on localhost:601. ```shell docker run --log-driver=syslog --log-opt syslog-address=tcp://127.0.0.1:601 -d ubuntu /bin/sh -c 'while true; do echo Hello, World!; sleep 1; done' ``` -------------------------------- ### Start Scalyr Agent (Tarball Install) Source: https://app.scalyr.com/help/install-agent-linux Starts the Scalyr Agent after installation via a tarball. This command is run directly from the command line and requires the agent's bin directory to be in the system's PATH. ```bash scalyr-agent-2 start ``` -------------------------------- ### Start Scalyr Agent Service (Linux) Source: https://app.scalyr.com/help/install-agent-linux-quick-start-4 This command starts the Scalyr agent service on a Linux system. It requires root privileges (sudo). After execution, it verifies configuration and server connection, then starts the agent in the background, monitoring for configuration changes. It also outputs a confirmation message upon successful startup. ```bash > sudo scalyr-agent-2 start ``` -------------------------------- ### Start Scalyr Agent (Shell) Source: https://app.scalyr.com/help/install-agent-linux This command starts the Scalyr agent service after installation or configuration. It is a crucial step to ensure the agent begins monitoring and sending logs. ```shell sudo scalyr-agent-2 start ``` -------------------------------- ### Python Installation Prompt (Linux) Source: https://app.scalyr.com/help/install-agent-linux An example error message indicating that Python is not installed, and providing commands to install it on Debian-based and RedHat-based systems. ```bash (Reading database ... 4797 files and directories currently installed.) Preparing to unpack .../scalyr-agent-2_2.1.1_all.deb ... Checking Python version. Suitable Python interpreter not found. You can install it by running command: 'apt install python' or apt install python3 ``` -------------------------------- ### Scalyr: Setting a Query Start Timestamp Source: https://app.scalyr.com/help/power-queries This example shows how to use the `querystart()` function to create a new column containing the query's start time. This is useful for time-based calculations or marking events with the query's initiation time. ```Scalyr Query Language status >= 400 status <= 499 | let errors.timestamp = querystart() ``` -------------------------------- ### Restart Scalyr Agent DaemonSet (kubectl) Source: https://app.scalyr.com/help/install-agent-kubernetes Re-applies the Scalyr Agent DaemonSet configuration to start or update the agent deployment. This command uses a URL to fetch the configuration, requiring network access. ```bash kubectl apply -f https://raw.githubusercontent.com/scalyr/scalyr-agent-2/release/k8s/no-kustomize/scalyr-agent-2.yaml ``` -------------------------------- ### Install Scalyr Agent using Shell Script Source: https://app.scalyr.com/help/install-agent-linux-quick-start-2 This snippet demonstrates how to download and execute the Scalyr Agent installation script. It requires a Log Write Access API key, which should be replaced with your actual key. The script installs the agent and configures it to send logs to the default US servers. ```shell > curl -sO https://www.scalyr.com/install-agent.sh > sudo bash ./install-agent.sh --set-api-key "[YOUR_API_KEY]" ``` -------------------------------- ### Scalyr Facet Query CLI Example Source: https://app.scalyr.com/help/api An example of using the Scalyr command-line interface (CLI) tool to perform a facet query. This command targets the 'uriPath' field from the 'accessLog' dataset, starting from one hour ago, with verbose output enabled. ```bash scalyr facet-query 'dataset="accessLog"' uriPath --start 1h --verbose ``` -------------------------------- ### Fresh Install Scalyr Agent AIO using Apt-Get (Shell) Source: https://app.scalyr.com/help/install-agent-linux This command installs the Scalyr Agent 'All in One' (AIO) package on a system that does not have the agent installed. It assumes the Scalyr repository is already configured or will be handled separately. This is for a clean installation. ```shell sudo apt-get install scalyr-agent-2-aio ``` -------------------------------- ### PowerQuery Examples Source: https://context7.com/context7/app_scalyr_help/llms.txt Examples demonstrating the use of the PowerQuery language for log analysis. ```APIDOC ## PowerQuery Examples ### Description This section provides various examples of how to use the Scalyr PowerQuery language to transform, filter, group, and analyze log data. ### Example 1: Count requests by status code Counts the number of requests for each HTTP status code and sorts them in descending order. ```javascript status >= 100 status < 600 | group count() by status | sort -count() ``` ### Example 2: Average latency by endpoint with threshold filtering Calculates the average latency for each endpoint, filtering for endpoints with more than 100 requests, and displays the top 20. ```javascript logfile='/var/log/nginx/access.log' | group avg_latency=avg(time), requests=count() by uriPath | filter requests > 100 | sort -avg_latency | limit 20 ``` ### Example 3: Error rate calculation Calculates the error rate (percentage of requests with status 500 or higher) over 5-minute intervals. ```javascript logfile='/var/log/nginx/access.log' | group total=count(), errors=count(status >= 500) by timebucket('5m') | let error_rate = (errors / total) * 100 | columns timestamp=timebucket, total, errors, error_rate ``` ### Example 4: Server health monitoring with parsed fields Monitors server health by counting errors and unique threads per server and level, after parsing log messages. ```javascript severity >= 4 | parse "^(?[^ ]+) (?\\w+) \[(?[^\\]+)\] (?.*?)( \\{(?
.*)\})?$" | group error_count=count(), unique_threads=estimate_distinct(thread) by serverHost, level | sort -error_count ``` ### Example 5: Top IP addresses by bandwidth consumption Identifies the top 50 IP addresses consuming the most bandwidth, calculating total MB transferred. ```javascript logfile='/var/log/nginx/access.log' status=200 | group requests=count(), total_bytes=sum(bytes), avg_bytes=avg(bytes) by ip | let total_mb = total_bytes / 1048576 | columns ip, requests, total_mb, avg_bytes | sort -total_mb | limit 50 ``` ### Example 6: P95 latency comparison across services Compares the 95th percentile latency across different services, aggregated hourly. ```javascript | group p95_latency=p95(time), request_count=count() by service, timebucket('1h') | transpose service ``` ``` -------------------------------- ### timeseriesQuery: CLI Example (scalyr tool) Source: https://app.scalyr.com/help/api This example demonstrates using the Scalyr command-line tool to execute a timeseries query. It specifies the query filter, start time, and number of buckets. The `--verbose` and `--output=json-pretty` flags are used for detailed JSON output. The `--token` flag is omitted, assuming the token is stored as an environment variable. ```shell scalyr timeseries-query "" --start 1h --buckets 60 --verbose --output=json-pretty ``` -------------------------------- ### Scalyr Full Configuration Example with Multiple Workers and Logs Source: https://app.scalyr.com/help/scalyr-agent-increase-throughput A comprehensive Scalyr agent configuration example demonstrating the setup of multiple teams ('messaging' and 'queue') with their respective API keys and log associations. It includes overriding default worker settings and assigning logs to specific workers. ```json { "api_key": "", "workers": [ { "id": "default", "sessions": 4 }, { "api_key": "", "id": "queue", "sessions": 2 } ], "logs": [ { "path": "/var/log/app/messaging/*.log" }, { "path": "/var/log/app/queue/*.log" "worker_id": "queue_team_key" } ] } ``` -------------------------------- ### Python Session and Event Batching Example Source: https://app.scalyr.com/help/api-integration Provides a Python example demonstrating how to create a session with a unique ID and structure a batch of events for the addEvents API. This includes session metadata and two sample events with timestamps and attributes. ```python import uuid guid = str(uuid.uuid4()) data = { "session": guid, "sessionInfo": { "serverHost": "syslog-01", "logfile": "win_dns", "parser":"pa-dns01", }, "events": [ { "ts": "{{currentdateNS}}", "attrs": { "message": "{\"message\": \"bar\", \"app\": \"hq\"}", "app": "hq" } }, { "ts": "{{currentdateNS}}", "attrs": { "message": "03/15/2022 02:54:59 PM 0BD8 PACKET UDP Snd 174.56.114.70 0002 R Q [0384 A NXDOMAIN] PTR x3.xclicks.net." } } ] } ``` -------------------------------- ### Scalyr Join Command Example: Transaction Events Source: https://app.scalyr.com/help/powerQueries This example demonstrates joining 'start_transaction' and 'end_transaction' events based on 'transaction_id'. It uses named queries 'start' and 'end' and renames fields for clarity. ```Scalyr Query Language | join start = ( message contains 'start_transaction' | columns timestamp, transaction_id, operation_name), end = ( message contains 'end_transaction' | columns timestamp, transaction_id, elapsed_time) on transaction_id ``` -------------------------------- ### Docker Daemon Configuration (syslog) Source: https://app.scalyr.com/help/install-agent-docker Example configuration for Docker's daemon.json file to enable the syslog logging driver. This driver forwards container logs to a syslog server. ```json { "log-driver": "syslog", "log-opts": { "syslog-address": "udp://127.0.0.1:12345" } } ``` -------------------------------- ### Scalyr Facet Query API GET Example (cURL) Source: https://app.scalyr.com/help/api Shows how to execute a facet query using cURL with the GET method, passing parameters directly in the URL. Note the encoding required for special characters like double quotes (%22) and spaces (%20). ```bash curl -X GET 'https://app.scalyr.com/api/facetQuery?queryType=facet&field=ip&startTime=24h&token=XXX' ``` -------------------------------- ### timeseriesQuery: POST Request Example (curl) Source: https://app.scalyr.com/help/api This example demonstrates how to make a POST request to the timeseriesQuery API using curl. It includes authorization, content type, and a JSON payload with multiple queries, specifying filters, start times, functions, and buckets. Note the escaped double quotes and the space after the backslash which needs removal for a proper request. ```shell curl -X POST https://app.scalyr.com/api/timeseriesQuery \ -H "Authorization: Bearer {token}" \ -H "Content-Type: application/json" \ -d '{ "queries": [ { "filter": "serverHost contains \"frontend\"", "startTime": "1h", "function": "count", "buckets": 60 },{ "filter": "serverHost contains \"frontend\"", "startTime": "1h", "function": "rate", "buckets": 60 }, ] }' ``` -------------------------------- ### Install Scalyr Agent AIO using Apt-Get (Shell) Source: https://app.scalyr.com/help/install-agent-linux These commands are used to install the Scalyr Agent 'All in One' (AIO) package on systems using the APT package manager. It includes updating package lists, removing any existing agent version, and then installing the AIO package. ```shell sudo apt-get update sudo apt-get remove scalyr-agent-2 sudo apt-get install scalyr-agent-2-aio ``` -------------------------------- ### Install Scalyr Agent using apt-get (Debian/Ubuntu) Source: https://app.scalyr.com/help/install-agent-linux Installs the Scalyr Agent on Debian-based systems. This process involves adding the Scalyr public key, configuring the repository, updating package lists, and finally installing the agent package. Ensure you have 'sudo' privileges. ```bash sudo apt-key add scalyr.key sudo add-apt-repository "deb https://scalyr-repo.s3.amazonaws.com/stable/apt scalyr main" sudo apt-get update sudo apt-get install scalyr-agent-2 ``` -------------------------------- ### Kubernetes: Create Scalyr Namespace Source: https://app.scalyr.com/help/install-agent-kubernetes Creates a dedicated Kubernetes namespace named 'scalyr' for the Scalyr Agent. This helps in organizing and managing Scalyr-related resources within the cluster. ```kubernetes kubectl create namespace scalyr ``` -------------------------------- ### Docker Daemon Configuration (json-file) Source: https://app.scalyr.com/help/install-agent-docker Example configuration for Docker's daemon.json file to enable the json-file logging driver. This driver captures container stdout and stderr as JSON. ```json { "log-driver": "json-file", "log-opts": { "max-size": "10m", "max-file": "3" } } ``` -------------------------------- ### Fresh Install Scalyr Agent AIO using Yum (Shell) Source: https://app.scalyr.com/help/install-agent-linux This command installs the Scalyr Agent 'All in One' (AIO) package on a system that does not have the agent installed. It assumes the Scalyr repository is already configured or will be handled separately. This is for a clean installation. ```shell sudo yum install scalyr-agent-2-aio ``` -------------------------------- ### Install Scalyr Agent Python Package - Shell Source: https://app.scalyr.com/help/creating-a-monitor-plugin This command installs the Scalyr Agent Python package locally within the activated virtual environment using pip. This step is necessary to develop and test agent plugins. ```shell $ pip install scalyr-agent-2 ``` -------------------------------- ### Scalyr Transpose Command Examples Source: https://app.scalyr.com/help/power-queries Illustrates practical applications of the 'transpose' command with different configurations. Examples show transposing a single column, using a timestamp as a key, and using multiple columns as keys. ```Scalyr Query Language | transpose serverHost | transpose serverHost on timestamp | transpose serverHost on year, month, day ``` -------------------------------- ### Install Scalyr Agent using Apt-Get (Shell) Source: https://app.scalyr.com/help/install-agent-linux This snippet installs the Scalyr agent using the APT package manager on Debian-based systems. It includes updating package lists, adding the Scalyr repository with the GPG key, and then installing the agent package. ```shell sudo apt-get update # Add the repository echo "deb [signed-by=/usr/share/keyrings/scalyr.gpg] https://scalyr-repo.s3.amazonaws.com/stable/apt scalyr main" | sudo tee /etc/apt/sources.list.d/scalyr.list sudo apt-get update sudo apt-get install scalyr-agent-2 ``` -------------------------------- ### Start Scalyr Agent (Linux) Source: https://app.scalyr.com/help/scalyr-agent This command is used on Linux systems to start the Scalyr Agent if it is not running. This is a crucial step in troubleshooting data collection issues. ```bash sudo scalyr-agent-2 start ``` -------------------------------- ### Start Scalyr Agent (Windows) Source: https://app.scalyr.com/help/scalyr-agent This command is used on Windows systems to start the Scalyr Agent if it is not running. This is a crucial step in troubleshooting data collection issues. ```bash scalyr-agent-2.exe start ``` -------------------------------- ### Install Scalyr Agent using Tarball Source: https://app.scalyr.com/help/install-agent-linux Installs the Scalyr Agent by downloading and extracting a tarball. This method is suitable for systems where package managers are not preferred or available. It includes setting the agent's bin directory to the system's PATH. ```bash wget -q https://app.scalyr.com/scalyr-repo/stable/latest/scalyr-agent-2.2.18.tar.gz tar --no-same-owner -zxf scalyr-agent-2.2.18.tar.gz mv scalyr-agent-2.2.18 scalyr-agent-2 export PATH=${PWD}/scalyr-agent-2/bin:$PATH ``` -------------------------------- ### Scalyr Alert Trigger Expression Examples Source: https://app.scalyr.com/help/alerts Demonstrates various Scalyr trigger expressions for alerting based on different metrics and conditions. These examples showcase the syntax for functions, time windows, filters, and thresholds. ```Scalyr Query Language count:1m(error) > 10 ``` ```Scalyr Query Language mean:5m(bytes where path == '/home') < 100 ``` ```Scalyr Query Language p[95]:1h(latency where path == '/home') >= 1000 ``` ```Scalyr Query Language count:1m('server error') > count:1m(success) * 0.1 ``` ```Scalyr Query Language mean:1m(latency where path == '/home') > 200 && count:1m(path == '/home') >= 20 ``` ```Scalyr Query Language max:1h(metric='df.1kblocks.free' mount='/' serverHost='localhost') - min:5m(metric='df.1kblocks.free' mount='/' serverHost='localhost') > 1000000 ``` ```Scalyr Query Language max:1h(metrics='proc.loadavg.1min' serverHost='localhost') / max:1h:1w(metrics='proc.loadavg.1min' serverHost='localhost') >= 2 ``` -------------------------------- ### Create and Navigate to Plugin Directory - Shell Source: https://app.scalyr.com/help/creating-a-monitor-plugin This command sequence creates a new directory named 'my-plugin' and then changes the current working directory to it, preparing the environment for agent plugin development. ```shell $ mkdir ~/my-plugin $ cd ~/my-plugin ``` -------------------------------- ### Kubernetes: Get Scalyr Agent Pods Source: https://app.scalyr.com/help/install-agent-kubernetes Lists all pods within the 'scalyr' namespace. This command is used to identify the specific pod name of a running Scalyr Agent instance, which is required for checking its status. ```kubernetes kubectl get pods --namespace=scalyr ``` -------------------------------- ### Scalyr Initial Filter Example Source: https://app.scalyr.com/help/powerQueries Demonstrates how the initial part of a Scalyr query before the first pipe acts as a filter. This example shows a simple filter on the 'status' field. ```Scalyr Query Language status=404 | group count() ``` -------------------------------- ### API Response Examples (JSON) Source: https://app.scalyr.com/help/api Illustrates common JSON response formats from the Scalyr API. This includes examples for successful operations, client-side errors, and server-side internal errors. It highlights the structure of the 'status' and 'message' fields. ```json { "status": "error/client", "message": "a human-readable message" } ``` ```json { "status": "error/server", "message": "a human-readable message" } ``` -------------------------------- ### Install Scalyr Agent via .sh Script (Linux) Source: https://app.scalyr.com/help/install-agent-linux Installs the Scalyr Agent using a shell script. Requires a 'Log Write Access' API Key and uses curl to download and execute the script. It sets the API key during installation. ```bash curl -sO https://www.scalyr.com/install-agent.sh sudo bash ./install-agent.sh --set-api-key "[YOUR_API_KEY]" ``` -------------------------------- ### Create Example Data with 'union' and 'columns' Source: https://app.scalyr.com/help/power-queries Demonstrates how to create a sample data table by using the 'union' command to combine results from multiple 'limit 1 | columns' queries. This is useful for generating example datasets for testing or demonstration. ```Scalyr Query Language | union ( | limit 1 | columns status=404, serverHost='web1', region='us-east-1' ), ( | limit 1 | columns status=200, serverHost='web2', region='us-east-1' ), ( | limit 1 | columns status=404, serverHost='web3', region='us-east-1' ), ( | limit 1 | columns status=404, serverHost='web1', region='us-west-1' ), ( | limit 1 | columns status=200, serverHost='web2', region='us-west-1' ) ``` -------------------------------- ### Configure ECS Instance User Data (Bash) Source: https://app.scalyr.com/help/install-agent-ecs This script is executed when an ECS instance starts. It configures the ECS cluster name, installs necessary tools like aws-cli and jq, and sets up a local script to automatically start tasks on instance startup. ```bash #!/bin/bash cluster="" echo ECS_CLUSTER=$cluster >> /etc/ecs/ecs.config start ecs yum install -y aws-cli jq instance_arn=$(curl -s http://localhost:51678/v1/metadata | jq -r '. | .ContainerInstanceArn' | awk -F/ '{print $NF}' ) az=$(curl -s http://instance-data/latest/meta-data/placement/availability-zone) region=${az:0:${#az} - 1} task="scalyr-agent" echo "cluster=$cluster az=$az region=$region aws ecs start-task --cluster $cluster --task-definition $task --container-instances $instance_arn --region $region" >> /etc/rc.local ``` -------------------------------- ### Install Scalyr Agent Kubernetes DaemonSet Source: https://app.scalyr.com/help/install-agent-kubernetes-pre2047 This command deploys the Scalyr Agent as a DaemonSet in your Kubernetes cluster, ensuring an agent runs on each node. It fetches the DaemonSet definition from a remote URL. ```bash kubectl create -f https://raw.githubusercontent.com/scalyr/scalyr-agent-2/release/k8s/scalyr-agent-2.yaml ``` -------------------------------- ### Run a Sample Alpine Container with Labels Source: https://app.scalyr.com/help/scalyr-agent-docker This command launches a basic Alpine Linux container that continuously outputs 'Hello World!'. It defines several Docker labels, including `tier`, `app`, `region`, and `unimportant-label`, to demonstrate how the Scalyr Agent can selectively attach them as attributes based on include filters. ```docker docker run -d --name barebones-container -l tier=production -l app=nginx -l region=us -l unimportant-label=some-value alpine /bin/sh -c 'while true; do echo Hello World!; sleep 1; done' ``` -------------------------------- ### timeseriesQuery: GET Request Example (curl) Source: https://app.scalyr.com/help/api This example shows how to make a GET request to the timeseriesQuery API using curl. Arguments are passed in the URL, requiring URL encoding for special characters like double quotes (`%22`), single quotes (`%27`), and spaces (`%20`). Only one query can be sent per request. ```shell curl -X GET 'https://app.scalyr.com/api/timeseriesQuery?filter=%22%22&startTime=1h&buckets=60&token=XXX' ``` -------------------------------- ### Scalyr Sort Command Examples Source: https://app.scalyr.com/help/powerQueries Examples demonstrating how to use the 'sort' command to order results, such as sorting by 'error_rate' in descending order or by 'percent_of_total' for 'requests_real' in ascending order. ```Scalyr Query Language | sort -error_rate | sort -percent_of_total(requests_real) ``` -------------------------------- ### Kubernetes: Migrate Secret to Scalyr Namespace Source: https://app.scalyr.com/help/install-agent-kubernetes Moves the Scalyr API key secret from the 'default' namespace to the 'scalyr' namespace. This is part of the upgrade process for older agent installations. ```kubernetes kubectl get secret scalyr-api-key --namespace=default --export -o yaml | kubectl apply -f - --namespace=scalyr ``` -------------------------------- ### Kubernetes: Migrate ConfigMap to Scalyr Namespace Source: https://app.scalyr.com/help/install-agent-kubernetes Moves the Scalyr configuration ConfigMap from the 'default' namespace to the 'scalyr' namespace. This is a necessary step when migrating older agent installations to the 'scalyr' namespace. ```kubernetes kubectl get configmap scalyr-config --namespace=default --export -o yaml | kubectl apply -f - --namespace=scalyr ``` -------------------------------- ### Scalyr Agent Installation using Docker Source: https://context7.com/context7/app_scalyr_help/llms.txt Deploys the Scalyr Agent as a Docker container. It supports basic deployment with an API key, custom server attributes, and mounting log directories. Advanced usage includes configuring log parsing and sampling rules via Docker labels. ```bash # Basic Docker agent deployment docker run -d \ --name scalyr-docker-agent \ -e SCALYR_API_KEY="YOUR_LOG_WRITE_API_KEY" \ -v /var/run/docker.sock:/var/scalyr/docker.sock \ -v /var/lib/docker/containers:/var/lib/docker/containers \ scalyr/scalyr-docker-agent ``` ```bash # Docker agent with custom configuration docker run -d \ --name scalyr-docker-agent \ -e SCALYR_API_KEY="YOUR_LOG_WRITE_API_KEY" \ -e SCALYR_SERVER_ATTRIBUTES='{"region":"us-east-1","environment":"production"}' \ -v /var/run/docker.sock:/var/scalyr/docker.sock \ -v /var/lib/docker/containers:/var/lib/docker/containers \ -v /var/log:/var/log:ro \ -v /path/to/agent.d:/etc/scalyr-agent-2/agent.d \ scalyr/scalyr-docker-agent ``` ```bash # Docker agent with labels for container selection docker run -d \ --name myapp \ --label com.scalyr.config.log.parser=customApp \ --label com.scalyr.config.log.sampling_rules='[{"match_expression":"DEBUG","sampling_rate":0.1}]' \ myapp:latest ``` -------------------------------- ### Kubernetes: Upgrade Scalyr Agent (Default Namespace) Source: https://app.scalyr.com/help/install-agent-kubernetes Upgrades the Scalyr Agent while keeping the installation in the 'default' namespace. This involves applying the service account and DaemonSet configurations specific to the default namespace. ```kubernetes kubectl apply -f https://raw.githubusercontent.com/scalyr/scalyr-agent-2/release/k8s/default-namespace/scalyr-service-account.yamlkubectl apply -f https://raw.githubusercontent.com/scalyr/scalyr-agent-2/release/k8s/default-namespace/scalyr-agent-2.yaml ``` -------------------------------- ### Run Scalyr Agent Plugin with `run_monitor.py` Source: https://app.scalyr.com/help/creating-a-monitor-plugin Execute an agent plugin for testing and debugging using the `run_monitor.py` script. This tool simulates the Scalyr Agent environment, directing all output to stdout and adjusting the sample interval for faster testing. It allows for custom configurations to be passed to the plugin. ```shell python -m scalyr_agent.run_monitor random_coin_monitor ``` ```shell python -m scalyr_agent.run_monitor -c "{ gauss_mean:8.5 }" random_coin_monitor ``` ```shell python -m scalyr_agent.run_monitor -h ``` -------------------------------- ### Set Up Python Virtual Environment - Shell Source: https://app.scalyr.com/help/creating-a-monitor-plugin These commands set up a isolated Python development environment using 'virtualenv' or 'venv'. It creates an environment named 'ENV', activates it, and provides instructions on how to deactivate it. ```shell $ virtualenv ENV # or $ python3 -m venv ENV # Python >= 3.6 $ source ENV/bin/activate $ deactivate ``` -------------------------------- ### Kubernetes: Delete Old Scalyr Agent Deployment (Default Namespace) Source: https://app.scalyr.com/help/install-agent-kubernetes Deletes the Scalyr Agent deployment that was installed in the 'default' namespace. This prevents running duplicate agent instances during an upgrade to the 'scalyr' namespace. ```kubernetes kubectl delete -f https://raw.githubusercontent.com/scalyr/scalyr-agent-2/release/k8s/default-namespace/scalyr-agent-2.yaml ``` -------------------------------- ### Numeric Query using cURL (GET Request) Source: https://app.scalyr.com/help/api This example shows how to execute a numeric query using cURL with a GET request. Parameters are appended to the URL, requiring proper encoding for special characters like double quotes, single quotes, and spaces. ```bash curl -X GET 'https://app.scalyr.com/api/numericQuery?queryType=numeric&startTime=1h&buckets=60&token=XXX' ``` -------------------------------- ### Manage Configuration Files using Scalyr API Source: https://context7.com/context7/app_scalyr_help/llms.txt Create, read, update, and delete configuration files for dashboards, parsers, and datatables. Separate API keys are required for read and write access. Includes examples for retrieving, updating, and listing files. ```bash # Get a dashboard configuration curl -X POST https://app.scalyr.com/api/getFile \ -H "Authorization: Bearer YOUR_CONFIG_READ_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "path": "/dashboards/System Metrics" }' ``` ```bash # Update a parser configuration curl -X POST https://app.scalyr.com/api/putFile \ -H "Authorization: Bearer YOUR_CONFIG_WRITE_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "path": "/logParsers/customApp", "content": "{patterns: [{pattern: \"^(?[^ ]+) (?\\\\w+) (?.*)$\\"}]}" }' ``` ```bash # List all configuration files curl -X POST https://app.scalyr.com/api/listFiles \ -H "Authorization: Bearer YOUR_CONFIG_READ_API_KEY" \ -H "Content-Type: application/json" \ -d '{}' ``` -------------------------------- ### Clone Scalyr Agent Repository (Bash) Source: https://app.scalyr.com/help/creating-a-monitor-plugin Clones the 'scalyr-agent-2' Git repository to the user's home directory. This is the first step for developing plugins directly from the source code. It uses the 'release' branch for stability. ```bash cd ~/ git clone https://github.com/scalyr/scalyr-agent-2.git --branch release ``` -------------------------------- ### Run Test Agent Plugin - Python Source: https://app.scalyr.com/help/creating-a-monitor-plugin This command executes a built-in test plugin for the Scalyr Agent. It helps verify that the development environment and agent package installation are set up correctly by outputting results from uniform and Gaussian distributions. ```python $ python3 -m scalyr_agent.run_monitor -c "{ gauss_mean: 0.5 }" scalyr_agent.builtin_monitors.test_monitor ``` -------------------------------- ### Querying Logs via cURL (GET Request) Source: https://app.scalyr.com/help/api Example of how to query log events from Scalyr using a cURL GET request. This method passes query arguments directly in the URL, requiring URL encoding for special characters like double quotes (%22), single quotes (%27), and spaces (%20). ```bash curl -X GET 'https://app.scalyr.com/api/query?queryType=log&maxCount=1&token=XXX' ``` -------------------------------- ### GET /listFiles Source: https://app.scalyr.com/help/api This endpoint retrieves all configuration files. ```APIDOC ## GET /listFiles ### Description Retrieve a list of all configuration files currently managed within your Scalyr account. ### Method GET ### Endpoint /api/v2/listFiles ### Parameters #### Query Parameters - **apiKey** (string) - Required - Your Scalyr API key with 'Configuration Read Access'. ### Request Example (No request body for GET requests) ### Response #### Success Response (200) - **status** (string) - Indicates the success status ('success'). - **files** (array) - An array of file path strings. #### Response Example ```json { "status": "success", "files": [ "/apps/my_app/config.json", "/global/settings.yml" ] } ``` ``` -------------------------------- ### Scalyr PowerQuery GET Request (curl) Source: https://app.scalyr.com/help/api This example demonstrates using a GET request to the Scalyr API's powerQuery endpoint. Arguments are passed directly in the URL, and special characters like double quotes, single quotes, and spaces must be URL-encoded. This method is suitable for simpler queries or when constructing URLs programmatically. ```shell curl -X GET 'https://app.scalyr.com/api/powerQuery?query=%22status%20%62%61%20100%20status%20%60%61599%20%124%20group%20count%40%41%20by%20status%22&token=XXX' ``` -------------------------------- ### Scalyr Agent Plugin: Random Coin Monitor Example Source: https://app.scalyr.com/help/creating-a-monitor-plugin This Python code defines a sample Scalyr agent plugin named 'RandomCoinMonitor'. It inherits from ScalyrMonitor and implements methods to initialize configuration parameters and gather sample metrics. The plugin emits two types of metrics: 'uniform' (random float between 0 and 1) and 'gauss' (random float from a Gaussian distribution), along with a counter for the number of samples. ```python import random from scalyr_agent import ScalyrMonitor class RandomCoinMonitor(ScalyrMonitor): def _initialize(self): self.__counter = 0 # Read two optional config fields. You may also create a required # configuration field by supplying the argument # ‘require_field=True’. Then, if the user does not supply the # field in the monitor’s configuration, an exception will be raised. self.__gauss_mean = self._config.get('gauss_mean', default=0.5, convert_to=float, min_value=0, max_value=10) self.__gauss_stddev = self._config.get('gauss_stddev', default=0.25, convert_to=float, min_value=0, max_value=5) def gather_sample(self): self.__counter += 1 self._logger.emit_value('uniform', random.random(), extra_fields={'count': self.__counter}) self._logger.emit_value('gauss', random.gauss(self.__gauss_mean, self.__gauss_stddev), extra_fields={'count': self.__counter}) ``` -------------------------------- ### Launch Scalyr Agent Container (JSON Driver with Modular Snippet) Source: https://app.scalyr.com/help/install-agent-docker Launches the Scalyr Agent container using the JSON logging driver and a modular agent.d snippet for API key configuration. This method mounts a pre-configured API key file into the agent's configuration. ```shell docker run -d --name scalyr-docker-agent -v /tmp/api_key.json:/etc/scalyr-agent-2/agent.d/api_key.json -v /var/run/docker.sock:/var/scalyr/docker.sock -v /var/lib/docker/containers:/var/lib/docker/containers scalyr/scalyr-agent-docker-json ``` -------------------------------- ### Get Array Length (Scalyr) Source: https://app.scalyr.com/help/power-queries Returns the total number of elements within an array. An empty array will return 0. Example: `Array.len()`. ```Scalyr | let Array=array(1, 2, 'three', null) | let length=Array.len() ``` -------------------------------- ### Scalyr Agent Monitor Configuration Example Source: https://app.scalyr.com/help/creating-a-monitor-plugin An example of how to configure a custom agent plugin within the Scalyr Agent's configuration file. This involves adding an entry to the 'monitors' section, specifying the module name and any required parameters for the plugin. ```json { ... monitors: [ { module: "random_coin_test", gauss_mean: 8.5, gauss_stddev: 5.0 }] ... } ``` -------------------------------- ### Configure Scalyr API Key (Modular Snippet) Source: https://app.scalyr.com/help/install-agent-docker Configures the Scalyr API key using a modular agent.d snippet. This involves creating a JSON file (e.g., /tmp/api_key.json) containing the API token and mounting it into the agent's configuration directory. ```json [Log in to see your API token.] ``` -------------------------------- ### Scalyr Query: Creating Sample Data with Union and Limit Source: https://app.scalyr.com/help/powerQueries This Scalyr query example illustrates how to create a sample table using the 'union' command combined with 'limit' and 'columns'. It generates a set of rows with specified status, serverHost, and region, useful for testing or demonstration purposes. ```scalyr | union ( | limit 1 | columns status=404, serverHost='web1', region='us-east-1' ), // Each "| limit" query finds 1 row of data, ( | limit 1 | columns status=200, serverHost='web2', region='us-east-1' ), // then overwrites it with "columns" ( | limit 1 | columns status=404, serverHost='web3', region='us-east-1' ), // "union" them together for a sample table. ( | limit 1 | columns status=404, serverHost='web1', region='us-west-1' ), ( | limit 1 | columns status=200, serverHost='web2', region='us-west-1' ) ``` -------------------------------- ### Stop Scalyr Agent DaemonSet (kubectl) Source: https://app.scalyr.com/help/install-agent-kubernetes Removes the Scalyr Agent DaemonSet from the Kubernetes cluster. This command requires kubectl to be configured for the target cluster and namespace. ```bash kubectl delete daemonset scalyr-agent-2 --namespace=scalyr ``` -------------------------------- ### Manage Users and Teams using Scalyr API Source: https://context7.com/context7/app_scalyr_help/llms.txt Manage users, groups, and permissions programmatically using Scalyr's API. Requires appropriate configuration write API keys. Includes examples for inviting users, listing users, and creating groups. ```bash # Invite a user curl -X POST https://app.scalyr.com/api/inviteUser \ -H "Authorization: Bearer YOUR_CONFIG_WRITE_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "emailAddress": "user@company.com", "permission": "readLog", "allowedSearch": "" }' ``` ```bash # List all users curl -X POST https://app.scalyr.com/api/listUsers \ -H "Authorization: Bearer YOUR_CONFIG_READ_API_KEY" \ -H "Content-Type: application/json" \ -d '{}' ``` ```bash # Create a group with limited permissions curl -X POST https://app.scalyr.com/api/addGroup \ -H "Authorization: Bearer YOUR_CONFIG_WRITE_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "name": "Customer Service", "permission": "limited", "allowedSearch": "tag=\"customer_service\"", "allowedDashboards": ["Customer Metrics", "Support Queue"] }' ``` -------------------------------- ### Get Minimum Value in Array (Scalyr) Source: https://app.scalyr.com/help/power-queries Returns the smallest numeric value found within the array. Returns `NaN` if the array contains any non-numeric values. Example: `Array.min()`. ```Scalyr | let Array=array(100, 200, 400) | let minimum=Array.min() ``` ```Scalyr | let Array=array(100, 200, 'pumpkin') | let minimum=Array.min() ``` -------------------------------- ### Run Scalyr Agent with Docker Labels as Attributes Source: https://app.scalyr.com/help/scalyr-agent-docker This command starts a Scalyr Agent Docker container. It enables the `labels_as_attributes` feature, specifies which labels to include using globs, sets a prefix for the attribute keys, and mounts the Docker socket and container directories. The API key is also provided. ```docker docker run -d --name scalyr-docker-agent -e SCALYR_API_KEY= -e SCALYR_LABELS_AS_ATTRIBUTES=true -e SCALYR_LABEL_INCLUDE_GLOBS=tier,app,region -e SCALYR_LABEL_PREFIX=dl_ -v /var/run/docker.sock:/var/scalyr/docker.sock -v /var/lib/docker/containers:/var/lib/docker/containers scalyr/scalyr-agent-docker-json:latest ``` -------------------------------- ### Get Maximum Value in Array (Scalyr) Source: https://app.scalyr.com/help/power-queries Calculates and returns the maximum numeric value present in the array. Returns `NaN` if the array contains non-numeric values. Example: `Array.max()`. ```Scalyr | let Array=array(100, 200, 400) | let maximum=Array.max() ``` ```Scalyr | let Array=array(100, 200, 'pumpkin') | let maximum=Array.max() ``` -------------------------------- ### Launch Scalyr Agent Container (Syslog Driver with Modular Snippet) Source: https://app.scalyr.com/help/install-agent-docker Launches the Scalyr Agent container using the Syslog logging driver and a modular agent.d snippet for API key configuration. This mounts a pre-configured API key file and maps port 601. ```shell docker run -d --name scalyr-docker-agent -v /tmp/api_key.json:/etc/scalyr-agent-2/agent.d/api_key.json -v /var/run/docker.sock:/var/scalyr/docker.sock -p 601:601 scalyr/scalyr-agent-docker-syslog ``` -------------------------------- ### Scalyr Expression Syntax Examples Source: https://app.scalyr.com/help/power-queries Demonstrates various components of Scalyr's expression syntax, including comparisons, string containment, function application, and boolean logic. ```Scalyr Query Language severity > 4 machine contains 'staging' mean(latency) ``` ```Scalyr Query Language expression contains ('search-term-1', 'search-term-2') expression matches ('regex-1', 'regex-2') message matches "\\d+" ``` ```Scalyr Query Language serverHost k8s-controller k8s - controller field\#name :message let kbPerSec = size / time ``` ```Scalyr Query Language | union (| limit 1| columns A="", B='foo', C='bar'), (| limit 1| columns A=0, B='foo', C='bar'), (| limit 1| columns A=null, B='foo', C='bar'), (| limit 1| columns A=false, B='foo', C='bar'), (| limit 1| columns A=NaN, B='foo', C='bar') | let x = (A || B || C) ``` -------------------------------- ### Disable Agent on Master Node (YAML) Source: https://app.scalyr.com/help/install-agent-kubernetes Comments out the toleration for master nodes in the DaemonSet configuration file to prevent the agent from running on them. This requires editing the DaemonSet YAML file. ```yaml tolerations: - key: "node-role.kubernetes.io/master" operator: "Exists" effect: "NoSchedule" ``` -------------------------------- ### Kubernetes: Apply Scalyr Agent ConfigMap Source: https://app.scalyr.com/help/install-agent-kubernetes Applies the Scalyr Agent configuration defined in the 'scalyr-agent-2-configmap.yaml' file to the Kubernetes cluster. This ConfigMap provides necessary settings for the agent's operation. ```kubernetes > kubectl create -f scalyr-agent-2-configmap.yaml ``` -------------------------------- ### Create or Update Configuration File with Curl Source: https://app.scalyr.com/help/api Provides a cURL example for creating or updating a configuration file using the putFile API. It includes the authorization token and the file path and content in the JSON payload. ```shell curl -X POST https://app.scalyr.com/api/putFile \ -H "Authorization: Bearer {token}" \ -H "Content-Type: application/json" \ -d '{ "path": "/dashboards/Foo", "content": "// My Foo Dashboard" }' ``` -------------------------------- ### Run DaemonSet on Select Nodes (Node Selector - YAML) Source: https://app.scalyr.com/help/install-agent-kubernetes Configures the DaemonSet to run only on nodes with a specific label using a nodeSelector. This snippet is part of the pod configuration within the DaemonSet definition. ```yaml spec: nodeSelector: logging: scalyr ```