Skip to content
The Chat Archive
gemma-3-27b-it· Google LMArena Human Preference 140K

mikrotik scripting, how do I get the usage value of the CPU on the terminal?

  1. User

    mikrotik scripting, how do I get the usage value of the CPU on the terminal?

  2. Assistant

    You have several ways to get the CPU usage on a Mikrotik router using scripting. Here are the most common and useful methods, along with explanations and examples:

    1. /system resource print (Most Common & Versatile)

    This is the most straightforward and generally preferred method. It provides a lot of information, including CPU usage.

    /system resource print
    

    This will output a lot of data. You'll need to parse the output to get just the CPU usage. Here's how you can do that in a script:

    :local cpu_usage [/system resource get cpu-load value]
    :log info "CPU Usage: $cpu_usage%"
    
    • /system resource get cpu-load value: This command specifically retrieves the CPU load percentage. The value parameter is crucial to get just the number.
    • :local cpu_usage ...: This stores the retrieved value in a local variable named cpu_usage.
    • :log info "CPU Usage: $cpu_usage%": This logs the CPU usage to the Mikrotik log. The $cpu_usage variable is expanded to its value. The % symbol is added to indicate it's a percentage.

    2. /system resource print detail (More Detailed, but more parsing)

    This provides even more detailed information, including per-core CPU usage. However, it requires more parsing.

    /system resource print detail
    

    To extract the overall CPU usage, you'll need to parse the output. This is more complex and less reliable than the first method because the output format can change with firmware updates. Here's an example, but be aware it might need adjustment:

    :local detail [/system resource print detail as-value]
    :local cpu_usage [:pick $detail 0 1]
    :log info "CPU Usage: $cpu_usage%"
    
    • /system resource print detail as-value: This prints the output in a key-value format, making parsing easier.
    • :local detail ...: Stores the output in a variable.
    • :local cpu_usage [:pick $detail 0 1]: This is the parsing part. It assumes the CPU usage is the second element (index 1) in the first row (index 0) of the output. This is fragile and can break if the output format changes.

    3. Using /tool fetch url and a Monitoring Service (For Historical Data)

    If you need to track CPU usage over time, you can use /tool fetch url to send the CPU usage to a monitoring service like Grafana, Zabbix, or Prometheus. This is more advanced and requires setting up a monitoring infrastructure.

    Example (sending to a simple HTTP endpoint):

    :local cpu_usage [/system resource get cpu-load value]
    :local url "http://your-monitoring-server/api/cpu_usage"
    :local post_data "cpu_usage=$cpu_usage"
    
    /tool fetch url=$url mode=http method=post http-data=$post_data
    
    • Replace http://your-monitoring-server/api/cpu_usage with the actual URL of your monitoring service.
    • The http-data parameter sends the CPU usage as a POST request.

    4. Using SNMP (For Integration with SNMP Monitoring Systems)

    Mikrotik supports SNMP. You can configure SNMP and then use an SNMP monitoring system to poll the CPU usage. This is a good option if you already have an SNMP-based monitoring infrastructure. The OID for CPU usage is typically .1.3.6.1.4.1.2021.11.9.0. (Check your Mikrotik documentation for the exact OID).

    Choosing the Right Method

    • For simple, one-time checks or basic scripting: Use /system resource print and the first example script. It's the easiest and most reliable.
    • For more detailed information (per-core usage): Use /system resource print detail, but be prepared to adjust the parsing script if the output format changes.
    • For historical data and monitoring: Use /tool fetch url to send the data to a monitoring service.
    • For integration with existing SNMP infrastructure: Use SNMP.

    Important Considerations:

    • Output Format Changes: The output format of /system resource print and /system resource print detail can change with Mikrotik RouterOS updates. Always test your scripts after upgrading RouterOS.
    • Script Performance: Parsing the output of /system resource print detail can be relatively slow, especially if you're doing it frequently. The /system resource get method is much faster.
    • Logging: Use :log info or :log warning to log the CPU usage to the Mikrotik log. This is helpful for troubleshooting and monitoring.
    • Error Handling: Add error handling to your scripts to gracefully handle cases where the CPU usage cannot be retrieved. For example, check if the /system resource command returns an error.

    Example Script with Error Handling:

    :local cpu_usage
    :local result [/system resource get cpu-load value]
    
    :if ($result = "") do={
      :log warning "Failed to get CPU usage."
    } else={
      :set cpu_usage $result
      :log info "CPU Usage: $cpu_usage%"
    }
    

    This script checks if the cpu-load value is empty. If it is, it logs a warning message. Otherwise, it sets the cpu_usage variable and logs the CPU usage. This makes the script more robust.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.