Zabbix Reading the Api - php

I'm getting Informations from the Zabbix Api with a PHP library. At the moment I get the "lastvalue" from the json Array:
try {
// connect to Zabbix-API
$api = new ZabbixApi($api_url, $username, $password);
$params = array(
'groupids' => '2',
'real_items' =>TRUE,
'monitored_items' =>TRUE,
'search' => array('name' => 'Disk root used p'),
'selectFunctions' => 'extend',
'output' => 'extend',
'sortfield' => 'name',
'limit' => '1'
);
$trends = $api->itemGet($params); // get data from api
$names = array();
foreach($trends as $trend) { // loop through the returned data
$names[] = $trend->lastvalue;
}
//print_r($names);
} catch(Exception $e) {
// Exception in ZabbixApi catched
echo $e->getMessage();
}
But now I want to get the "lastvalue" plus the "name" of the Item in this Array for example like that: "name"+"lastvalue". How can I get both of them into my array $names[]?

Here is my answer from my comments, hope its what you are looking for!
$trends = $api->itemGet($params);
$names = array();
foreach($trends as $trendKey => $trendValue)
{
$names[$trendKey] = $trendValue;
}
#Test the names array
foreach ($names as $nameKey => $nameValue)
{
print("{$nameKey} = {$nameValue}<br />");
}
Return value:
groupids = 2
real_items = TRUE
...
sortfield = name
limit = 1

Related

update batch in codeigniter

I have data on db_temporary here I will update based on id_service, but why doesn't the update work?
public function updateUpload() {
$db = $this->M_order->db_temporary_service(); //db_temporary
$data = array();
foreach($db AS $key => $val){
$data[] = array(
"id_service" => $_POST['id_service'][$key],
"id_destination" => $_POST['id_destination'][$key],
"id_sub_destination" => $_POST['id_sub_destination'][$key],
"charges_order" => $_POST['charges_order'][$key],
"weight_order" => $_POST['weight_order'][$key],
"jenis_service" => $_POST['jenis_service'][$key],
"service_order" => $_POST['service_order'][$key],
"charges_order" => $_POST['charges_order'][$key],
);
echo '<pre>', print_r($data);
//update to db_service where id_service
$this->db->update_batch('service', $data, 'id_service');
redirect('backend/.....');
}
}
The data doesn't seem to be updated because you've used update_batch as well as redirect inside foreach loop, I'm writing the rectified code below, comments are mentioned wherever necessary. See if it resolves your issue.
public function updateUpload(){
$db = $this->M_order->db_temporary_service(); //db_temporary
$data = array();
foreach($db AS $key => $val){
$data[] = array(
"id_service" => $_POST['id_service'][$key],
"id_destination" => $_POST['id_destination'][$key],
"id_sub_destination" => $_POST['id_sub_destination'][$key],
"charges_order" => $_POST['charges_order'][$key],
"weight_order" => $_POST['weight_order'][$key],
"jenis_service" => $_POST['jenis_service'][$key],
"service_order" => $_POST['service_order'][$key],
"charges_order" => $_POST['charges_order'][$key],
);
}
// echo '<pre>', print_r($data); // all the data should be here
$this->db->update_batch('service', $data, 'id_service'); // update the table with all the data
redirect('backend/.....');
}

Magento select query not looping properly using a foreach loop

I have written following code:
try {
$json = array('success' => true);
$read = $this->read;
$readresult = $read->fetchAll("SELECT * FROM brand");
foreach($readresult as $r) {
$json['brands'][] = array(
'id' => $r['brand_id'],
'name' => $r['name'],
'description' => $r['description'],
);
}
return $json;
} catch (Exception $e) {
$message = $e->getMessage();
echo json_encode(array("status" => "500", "error" => $message));
}
In this code I am trying to display all the brand records from the database table.
But the problem is when I am trying to output the result the it is only displaying one record.
Can anyone please check what is the problem.
The output of the code above is:
{
"success":true,
"products":[
{
"id":"4",
"name":"Test",
"href":"http:\/\/localhost:8‌​1\/magento\/index.php\/catalog\/product\/view\/id\/4\/",
"thumb":"http:\/\/localho‌​st:81\/magento\/media\/catalog\/product\/cache\/0\/thumbnail\/9df78eab33525d08d6e‌​5fb8d27136e95\/images\/catalog\/product\/placeholder\/thumbnail.jpg",
"pirce":"$11‌​1,111.00"
}
]}
try like this,
$json['brands'] = array();
foreach($readresult as $r) {
$brand = array(
'id' => $r['brand_id'],
'name' => $r['name'],
'description' => $r['description'],
);
array_push($json['brands'],$brand);
}

error while appending data into an array using array_push in php

I want to filter my data_array and create a new array $arr with new data.I tried using following function.it is not appending data. but it is replacing the data.I know that I have to use array_push(). when I did that data is appending to the array as single elements.
foreach ($data_array as $value) {
try {
$rep = $value->rep;
$idposition_rep = $value->idposition_rep;
$max_idinvoice = $value->max_idinvoice;
$idsession = $value->idsession;
$i_date = $value->i_date;
$last_i_time_string = $value->last_i_time;
$delayTime = $value->delayTime;
if ($delayTime > 30) {
$arr[] = array(
'rep' => $rep,
'idposition_rep' => $idposition_rep,
'max_idinvoice' => $max_idinvoice,
'idsession' => $idsession,
'i_date' => $i_date,
'last_i_time_string' => $last_i_time_string,
'curr_time' => date("H:i:s"),
'delayTime' => $delayTime);
}
} catch (PDOException $ex) {
echo $ex->getMessage();
die;
}
}
I had created sample code based on your requirement. replace as per your requirement with variables and assignment.
$data_array is sample input array.
$data_array = array('1' => 'abc', '2'=>'das', '3' => 'def');
$arr = array();
$i=0;
foreach ($data_array as $value) {
try {
$rep = $value;
$aval = strpos($rep,'a'); // Put Your condition for display Time > 30
if ($aval !== false) {
$arr[$i] = array(
'rep' => $rep // assign values get from input array.
);
$i++;
}
} catch (PDOException $ex) {
echo $ex->getMessage();
die;
} }
print_r($arr);
OUTPUT:
Array
(
[0] => Array
(
[rep] => abc
)
[1] => Array
(
[rep] => das
)
)
Hope this will help you.

Zabbix json get Array without json Object

I have following code to read Item names from the ZabbixAPI:
try {
// connect to Zabbix-API
$api = new ZabbixApi($api_url, $username, $password);
$params = array( 'groupids' => '2 ',
'real_items' =>TRUE,
'monitored_items' =>TRUE,
'search' => array('name' => 'Disk root used p'),
'selectFunctions' => 'extend',
'output' => 'name',
'sortfield' => 'name',
'lastvalue' => 'value'
);
$items = $api->itemGet($params); // get data from api
echo serialize($items);
foreach($items as $item) { // loop through the returned data
echo "<td>".$item."</td>";
}
} catch(Exception $e) {
// Exception in ZabbixApi catched
echo $e->getMessage();
}
With that I get this Output for every Item:
stdClass Object ( [itemid] => 81351 [name] => Disk root used p )
But I only need the 'name' of the Item and not the json objects so the output is just an array like that: itemname1, itemname2....
You can do the following:
$names = array();
foreach($items as $item) { // loop through the returned data
$names[] = $item->name;
}
The $names array will be the array of item names.

Retrieve fields of Mongo collection in PHP

I tried using the fields() method on the cursor:
<?php
$mongo = new Mongo("mongodb://localhost");
print_r($mongo);
$db = $mongo->test;
// access collection
$collection = $db->test;
// execute query
// retrieve all documents
print_r($test);
$cursor = $collection->find();
print_r($cursor);
$fields = $cursor->fields(array("summary" => true));
print_r($fields);
The output is:
Mongo Object ( [connected] => 1 [status] => [server:protected] => [persistent:protected] => )
MongoCursor Object ( )
MongoCursor Object ( )
Looks like the driver and the connection work but I can't retrieve the distinct summarization of the fields.
Assume that we have access to mongodb database with the name db, are going to retrieve data from MyCollection and use MongoDB\Driver\Manager:
$manager = new MongoDB\Driver\Manager( $DB_CONNECTION_STRING );
Then we retrieve data in this case by name with a limit 2 documents and want to get only fields name, age and address among many others. Projection option can be used to specify which fields should be returned using 0 to exclude and 1 to include:
$filter = ['name' => 'John'];
$options = [ 'projection' => ['_id' => 0, 'name' => 1, 'age' => 1, 'address' => 1], 'limit' => 2 ];
$query = new MongoDB\Driver\Query($filter, $options);
$manager->executeQuery('db.MyCollection', $query);
If we need to get all columns, then we simply omit projection option at all as the following:
$filter = ['name' => 'daniel'];
$options = [];
$query = new MongoDB\Driver\Query($filter, $options);
$manager->executeQuery('db.MyCollection', $query);
const HOST = 'localhost';
const PORT = 27017;
const DBNAME = 'test';
const COLLECTION = 'test';
$connectionString = sprintf('mongodb://%s:%d', HOST, PORT);
try {
$connection = new Mongo($connectionString);
$database = $connection->selectDB(DBNAME);
} catch (MongoConnectionException $e) {
throw $e;
}
$collection = $database->selectCollection(COLLECTION);
try{
$query = array();
$specifyKey = array("summary" => true);
$cursor = $collection->find($query , $specifyKey);
}catch(MongoException $e) {
die('Failed to find One data '.$e->getMessage());
}catch (MongoCursorException $e) {
echo "error message: ".$e->getMessage()."\n";
echo "error code: ".$e->getCode()."\n";
}
while ($cursor->hasNext()){
$nextCursor = $cursor->getNext();
//process next cursor
}
After the $cursor variable try an foreach instead. It goes through the cursor results.
foreach($cursor as $document) {
var_dump($document);
}

Categories