# Host Metrics on Ubuntu

> Monitor Ubuntu server metrics including CPU, memory, disk, network, and processes using OpenTelemetry collector with Last9

Source: https://last9.io/docs/integrations/observability/host-metrics-on-ubuntu/

Use OpenTelemetry to instrument host metrics.

## Prerequisites

Before setting up Ubuntu host metrics monitoring, ensure you have:

- Ubuntu server with administrative access
- OpenTelemetry Collector installation access
- Last9 account with integration credentials

1.  **Install OpenTelemetry Collector**

    You can install either `rpm` or `deb` package depending on flavor of operating system you are using.

    > Note: systemd is required for automatic service configuration.

**RPM Package**

    Installing rpm package:

    ```bash
    wget https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v0.118.0/otelcol-contrib_0.118.0_linux_amd64.rpm
    sudo rpm -ivh otelcol-contrib_0.118.0_linux_amd64.rpm
    ```

**DEB Package**

    Installing deb package:

    ```bash
    sudo apt-get update
    sudo apt-get -y install wget systemctl
    wget https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v0.118.0/otelcol-contrib_0.118.0_linux_amd64.deb
    sudo dpkg -i otelcol-contrib_0.118.0_linux_amd64.deb
    ```

    More installation options can be found [here](https://opentelemetry.io/docs/collector/installation/#linux).

2.  **Configure OpenTelemetry Collector**

    Create the configuration file:

    ```bash
    sudo nano /etc/otel-contrib-collector/config.yaml
    ```

    Use the following configuration for OpenTelemetry Collector:

    ```yaml
    receivers:
      filelog:
        # File path pattern to read system logs from.
        include: [/var/log/*.log]
        include_file_path: true
        retry_on_failure:
          enabled: true
      hostmetrics:
        collection_interval: 60s
        scrapers:
          cpu:
            metrics:
              system.cpu.logical.count:
                enabled: true
          memory:
            metrics:
              system.memory.utilization:
                enabled: true
              system.memory.limit:
                enabled: true
          load:
          disk:
          filesystem:
            metrics:
              system.filesystem.utilization:
                enabled: true
          network:
          paging:
          processes:
          process:
            mute_process_user_error: true
            mute_process_io_error: true
            mute_process_exe_error: true
            metrics:
              process.cpu.utilization:
                enabled: true
              process.memory.utilization:
                enabled: true

    processors:
      batch:
        timeout: 20s
        send_batch_size: 10000
        send_batch_max_size: 10000
      resourcedetection/system:
        detectors: ["system"]
        system:
          hostname_sources: ["os"]
          resource_attributes:
            host.ip:
              enabled: true
            host.cpu.model.name:
              enabled: true
      resourcedetection/cloud:
        detectors: ["aws", "gcp", "azure"]
      transform/hostmetrics:
        metric_statements:
          - context: datapoint
            statements:
              - set(attributes["host.name"], resource.attributes["host.name"])
              - set(attributes["cloud.account.id"], resource.attributes["cloud.account.id"])
              - set(attributes["cloud.availability_zone"], resource.attributes["cloud.availability_zone"])
              - set(attributes["cloud.platform"], resource.attributes["cloud.platform"])
              - set(attributes["cloud.provider"], resource.attributes["cloud.provider"])
              - set(attributes["cloud.region"], resource.attributes["cloud.region"])
              - set(attributes["host.type"], resource.attributes["host.type"])
              - set(attributes["host.image.id"], resource.attributes["host.image.id"])
      transform/logs:
        flatten_data: true
        log_statements:
          - context: resource
            statements:
              - set(attributes["service.name"], "hostmetrics") # Change this as needed
              - set(attributes["deployment.environment"], "staging") # Change this as needed

    exporters:
      otlp/last9:
        endpoint: "{{ .Logs.WriteURL }}"
        headers:
          "Authorization": "{{ .Logs.AuthValue }}"
      debug:
        verbosity: detailed

    service:
      pipelines:
        logs:
          receivers: [filelog]
          processors:
            [
              batch,
              resourcedetection/system,
              resourcedetection/cloud,
              transform/logs,
            ]
          exporters: [otlp/last9]
        metrics:
          receivers: [hostmetrics]
          processors:
            [
              batch,
              resourcedetection/system,
              resourcedetection/cloud,
              transform/hostmetrics,
            ]
          exporters: [otlp/last9]
    ```

3.  **Configure the OpenTelemetry Collector Service**

    Create the service file:

    ```bash
    sudo nano /etc/systemd/system/otelcol-contrib.service
    ```

    Add the following:

    ```bash
    [Unit]
    Description=OpenTelemetry Collector Contrib with custom flags
    After=network.target

    [Service]
    ExecStart=/usr/bin/otelcol-contrib --config /etc/otelcol-contrib/config.yaml --feature-gates transform.flatten.logs
    Restart=always
    User=root
    Group=root

    [Install]
    WantedBy=multi-user.target
    ```

4.  **Start the OpenTelemetry Collector Service**

    Reload systemd to apply changes and start the service:

    ```bash
    sudo systemctl daemon-reload
    sudo systemctl enable otelcol-contrib
    sudo systemctl start otelcol-contrib
    ```

## Verification

Verify Configuration and Monitor Logs:

Check the service status:

```bash
sudo systemctl status otelcol-contrib
```

Monitor logs in real-time:

```bash
sudo journalctl -u otelcol-contrib -f
```

Log into your Last9 account to confirm host metrics are being received.

## Need Help?

If you encounter any issues or have questions:

- Join our [Discord community](https://discord.com/channels/652153247672729619/652153247672729621) for real-time support
- Contact our support team at [support@last9.io](mailto:support@last9.io)
