mikrotik scripting, how do I get the usage value of the CPU on the terminal?
- User
mikrotik scripting, how do I get the usage value of the CPU on the terminal?
- 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 printThis 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. Thevalueparameter is crucial to get just the number.:local cpu_usage ...: This stores the retrieved value in a local variable namedcpu_usage.:log info "CPU Usage: $cpu_usage%": This logs the CPU usage to the Mikrotik log. The$cpu_usagevariable 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 detailTo 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 urland a Monitoring Service (For Historical Data)If you need to track CPU usage over time, you can use
/tool fetch urlto 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_usagewith the actual URL of your monitoring service. - The
http-dataparameter 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 printand 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 urlto 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 printand/system resource print detailcan change with Mikrotik RouterOS updates. Always test your scripts after upgrading RouterOS. - Script Performance: Parsing the output of
/system resource print detailcan be relatively slow, especially if you're doing it frequently. The/system resource getmethod is much faster. - Logging: Use
:log infoor:log warningto 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 resourcecommand 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-loadvalue is empty. If it is, it logs a warning message. Otherwise, it sets thecpu_usagevariable and logs the CPU usage. This makes the script more robust.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.