Community discussions

 
raymondr15
Member Candidate
Member Candidate
Topic Author
Posts: 118
Joined: Fri Sep 05, 2014 1:11 am
Location: East London, South Africa
Contact:

PHP API sometimes not returning all commands

Wed Aug 29, 2018 12:50 pm

Hi, I am running RouterOS 6.42.7 and have a PHP function that fetches the routers resource, voltage and identity
function get_routerOS_resource() {
		//Set globals
		global $API;
		global $Debug;

		$API->write('/system/resource/print',true);
		$API->write('/system/health/print',true);
		$API->write('/system/identity/print',true);
		$READ = $API->read(false);
		$ARRAY = $API->parseResponse($READ);
		if(count($ARRAY)>0){
			return($ARRAY);
		} else {
			return(false);
		}
	}
When running
$ARRAY = get_routerOS_resource();
print_r($ARRAY);
Sometimes only the (0) array gets returned or sometimes (0 and 1). Every now and then it skips some of the commands.


The normal output is:
[
{
0: {
uptime: "3d18h41m6s",
version: "6.42.7 (stable)",
build-time: "Aug/17/2018 09:48:44",
free-memory: "37236736",
total-memory: "67108864",
cpu: "MIPS 24Kc V7.4",
cpu-count: "1",
cpu-frequency: "400",
cpu-load: "80",
free-hdd-space: "112902144",
total-hdd-space: "134217728",
write-sect-since-reboot: "55539",
write-sect-total: "3034745",
bad-blocks: "0",
architecture-name: "mipsbe",
board-name: "RB Metal 5SHPn",
platform: "MikroTik"
},
1: {
voltage: "12.6",
temperature: "36"
},
2: {
name: "MikroTik"
}
}
]

The full output when only (0) returned with debug=true:
[
"Connection attempt #1 to ssl://10.3.1.2:8729...",
"<<< [6] /login",
"<<< [9] =name=API_USERNAME",
"<<< [19] =password=API_PASSWORD",
">>> [5/5] bytes read.",
">>> [5, 39]!done",
">>> [37/37] bytes read.",
">>> [37, 1]=ret=1c2c1d4c29d91b154f215fdcc26e26cf",
"<<< [6] /login",
"<<< [9] =name=API_USERNAME",
"<<< [44] =response=006900857dcb0a00eb47165ebde7d97064",
">>> [5/5] bytes read.",
">>> [5, 1]!done",
"Connected...",
"<<< [22] /system/resource/print",
"<<< [20] /system/health/print",
"<<< [22] /system/identity/print",
">>> [3/3] bytes read.",
">>> [3, 392]!re",
">>> [19/19] bytes read.",
">>> [19, 372]=uptime=3d18h47m17s",
">>> [24/24] bytes read.",
">>> [24, 347]=version=6.42.7 (stable)",
">>> [32/32] bytes read.",
">>> [32, 314]=build-time=Aug/17/2018 09:48:44",
">>> [21/21] bytes read.",
">>> [21, 292]=free-memory=37408768",
">>> [22/22] bytes read.",
">>> [22, 269]=total-memory=67108864",
">>> [19/19] bytes read.",
">>> [19, 249]=cpu=MIPS 24Kc V7.4",
">>> [12/12] bytes read.",
">>> [12, 236]=cpu-count=1",
">>> [18/18] bytes read.",
">>> [18, 217]=cpu-frequency=400",
">>> [12/12] bytes read.",
">>> [12, 204]=cpu-load=14",
">>> [25/25] bytes read.",
">>> [25, 178]=free-hdd-space=112902144",
">>> [26/26] bytes read.",
">>> [26, 151]=total-hdd-space=134217728",
">>> [30/30] bytes read.",
">>> [30, 120]=write-sect-since-reboot=55631",
">>> [25/25] bytes read.",
">>> [25, 94]=write-sect-total=3034837",
">>> [13/13] bytes read.",
">>> [13, 80]=bad-blocks=0",
">>> [25/25] bytes read.",
">>> [25, 54]=architecture-name=mipsbe",
">>> [26/26] bytes read.",
">>> [26, 27]=board-name=RB Metal 5SHPn",
">>> [18/18] bytes read.",
">>> [18, 8]=platform=MikroTik",
">>> [5/5] bytes read.",
">>> [5, 1]!done",
{
0: {
uptime: "3d18h47m17s",
version: "6.42.7 (stable)",
build-time: "Aug/17/2018 09:48:44",
free-memory: "37408768",
total-memory: "67108864",
cpu: "MIPS 24Kc V7.4",
cpu-count: "1",
cpu-frequency: "400",
cpu-load: "14",
free-hdd-space: "112902144",
total-hdd-space: "134217728",
write-sect-since-reboot: "55631",
write-sect-total: "3034837",
bad-blocks: "0",
architecture-name: "mipsbe",
board-name: "RB Metal 5SHPn",
platform: "MikroTik"
}
},
"Disconnected...",
"Disconnected..."
]
 
raymondr15
Member Candidate
Member Candidate
Topic Author
Posts: 118
Joined: Fri Sep 05, 2014 1:11 am
Location: East London, South Africa
Contact:

Re: PHP API sometimes not returning all commands  [SOLVED]

Thu Aug 30, 2018 9:42 am

I may have fixed my issue by changing the code around but still testing to so if this will happen again, so far its been a few hours and been working perfectly with the updated script:
function get_routerOS_resource() {
		//Set globals
		global $API;
		global $Debug;

		//Get resource
		$API->write('/system/resource/print',true);
		$READ = $API->read(false);
		$RESOURCE_ARRAY = $API->parseResponse($READ);
		
		
		//Get health
		$API->write('/system/health/print',true);
		$READ = $API->read(false);
		$HEALTH_ARRAY = $API->parseResponse($READ);
		
		//Get identity
		$API->write('/system/identity/print',true);
		$READ = $API->read(false);
		$IDENTITY_ARRAY = $API->parseResponse($READ);
		
		$ARRAY = array_merge($RESOURCE_ARRAY, $HEALTH_ARRAY, $IDENTITY_ARRAY);
		if(count($ARRAY)>0){
			return($ARRAY);
		} else {
			return(false);
		}
	}

Who is online

Users browsing this forum: No registered users and 14 guests