I want to use python oci package to get information about environment.
how to list all IPs addresses (both public or private) attached to compute node?
list_instances() does not provide this part of compute details unfortunately.
thanks.
As shared by #Char above, this oci-python-sdk example should help you here.
You can find a list of all supported services by SDK for Python here - https://docs.cloud.oracle.com/en-us/iaas/Content/API/SDKDocs/pythonsdk.htm
Additionally, full documentation for OCI Python SDK can be found here - https://docs.cloud.oracle.com/en-us/iaas/tools/python/2.21.5/
Please use this code. Basically you have to lookup VNIC Attachment object and filter VNIC_ID based on InstanceId. VNIC_ID can be used for looking up IP Addresses subsequently.
I have used data[0] to indicate the first attachment. You could use a loop to go through all attachments and print the IP.
compute_client = oci.core.ComputeClient(config={}, signer=signer)
network_client = oci.core.VirtualNetworkClient(
config={}, signer=signer)
vnic_id = compute_client.list_vnic_attachments(
cd_compartment_id, instance_id=instanceId).data[0].vnic_id
private_ip = network_client.get_vnic(vnic_id).data.private_ip
Assuming that you have the display name of an OCI compute instance and you need its private and public IP addresses, provided that you already created an API key in your profile and that you configured your ~/.oci/config with valid pem private key downloaded when creating the API Key, the following Python code can help:
import oci
display_name = "your display name"
config = oci.config.from_file()
identity = oci.identity.IdentityClient(config)
user = identity.get_user(config["user"]).data
instances = oci.core.ComputeClient(config).list_instances(
compartment_id=user.compartment_id).data
instance_id = {i.display_name: i.id for i in instances}[display_name]
compute_client = oci.core.ComputeClient(config)
vnic_data = compute_client.list_vnic_attachments(
compartment_id=user.compartment_id, instance_id=instance_id).data
network_client = oci.core.VirtualNetworkClient(config)
vnic_list = [network_client.get_vnic(vnic_attachment.vnic_id).data
for vnic_attachment in vnic_data]
public_ip = {i.display_name: i.public_ip for i in vnic_list}[display_name]
private_ip = {i.display_name: i.private_ip for i in vnic_list}[display_name]
print(public_ip, private_ip)
This implies:
pip3 install oci oci-cli
Related
I am using zabbix for infrastructure monitoring. I want to export alerts real time to a centralized platform like splunk, ELK?
Can i pull all active alerts for last 15 mins using Zabbix API
ALso, is the api mature enough to implement auto close functionality?
Thanks in Advance !!!!
You should use the problem.get api:
This method is for retrieving unresolved problems. It is also
possible, if specified, to additionally retrieve recently resolved
problems.
A small python sample:
from zabbix.api import ZabbixAPI
zabbixServer = 'http://yourserver/zabbix/'
zabbixUser = 'someuser'
zabbixPass = 'somepass'
zapi = ZabbixAPI(url=zabbixServer, user=zabbixUser, password=zabbixPass)
problems = zapi.problem.get()
for problem in problems:
trigger = zapi.trigger.get (triggerids=problem['objectid'], selectHosts='extend')
interface = zapi.hostinterface.get(hostids=trigger[0]['hosts'][0]['hostid'])
group = zapi.hostgroup.get(hostids=trigger[0]['hosts'][0]['hostid'])
enabled = "Enabled"
if (trigger[0]['hosts'][0]['status'] == "1"):
enabled = "Disabled"
print "Group:{}; Host:{}; IP:{}; Problem:{}; {}".format(group[1]['name'],
trigger[0]['hosts'][0]['host'],
interface[0]['ip'],
trigger[0]['description'],
enabled )
Motivation
Reduce the maintenance of an Azure DevOps task that invokes a Powershell script with a lot of parameters ("a lot" could be 5).
The idea relies on the fact that Azure DevOps generates environment variables to reflect the build variables. So, I devised the following scheme:
Prefix all non secret Azure DevOps variables with MyBuild.
The task powershell script would call a function to check the script parameters against the MyBuild_ environment variables and would automatically assign the value of the MyBuild_xyz environment variable to the script parameter xyz if the latter has no value.
This way the task command line would only contain secret parameters (which are not reflected in the environment). Often, there are no secret parameters and so the command line remains empty. We find this scheme to reduce the maintenance of the tasks driven by a powershell script.
Example
param(
$DBUser,
[ValidateNotNullOrEmpty()]$DBPassword,
$DBServer,
$Configuration,
$Solutions,
$ClientDB = $env:Build_DefinitionName,
$RawBuildVersion = $env:Build_BuildNumber,
$BuildDefinition = $env:Build_DefinitionName,
$Changeset = $env:Build_SourceVersion,
$OutDir = $env:Build_BinariesDirectory,
$TempDir,
[Switch]$EnforceNoMetadataStoreChanges
)
$ErrorActionPreference = "Stop"
. $PSScriptRoot\AutomationBootstrap.ps1
$AutomationScripts = GetToolPackage DevOpsAutomation
. "$AutomationScripts\vNext\DefaultParameterValueBinding.ps1" $PSCommandPath -Required 'ClientDB' -Props #{
OutDir = #{ DefaultValue = [io.path]::GetFullPath("$PSScriptRoot\..\..\bin") }
TempDir = #{ DefaultValue = 'D:\_gctemp' }
DBUser = #{ DefaultValue = 'SomeUser' }
}
The described parameter binding logic is implemented in the script DefaultParameterValueBinding.ps1 which is published in a NuGet package. The code installs the package and thus gets access to the script.
In the example above, some parameters default to predefined Azure Devops variables, like $RawBuildVersion = $env:Build_BuildNumber. Some are left uninitialized, like $DBServer, which means it would default to $env:MyBuild_DBServer.
We can get away without the special function to do the binding, but then the script author would have to write something like this:
$DBServer = $env:MyBuild_DBServer,
$Configuration = $env:MyBuild_Configuration,
$Solutions = $env:MyBuild_Solutions,
I wanted to avoid this, because of the possibility of an accidental name mismatch.
The Problem
The approach does not work when I package the logic of DefaultParameterValueBinding.ps1 into a module function. This is because of the module scope isolation - I just cannot modify the parameters of the caller script.
Is it still possible to do? Is it possible to achieve my goal in a more elegant way? Remember, I want to reduce the cost associated with maintaining the task command line in Azure DevOps.
Right now I am inclined to retreat back to this scheme:
$xyz = $(Resolve-ParameterValue 'xyz' x y z ...)
Where Resolve-ParameterValue would first check $env:MyBuild_xyz and if not found select the first not null value out of x,y,z,...
But if the Resolve-ParameterValue method comes from a module, then the script must assume the module has already been installed, because it has no way to install it before the parameters are evaluated. Or has it?
EDIT 1
Notice the command line used to invoke the DefaultParameterValueBinding.ps1 script does not contain the caller script parameters! It does include $PSCommandPath, which is used to obtain the PSBoundParameters collection.
Yea, but it will require modifications to the calling script and the function. Pass the parameters by reference. Adam B. has a nice piece on passing parameters by reference in the following:
https://mcpmag.com/articles/2015/06/04/reference-variables-in-powershell.aspx
Net-net, the following is an example:
$age = 12;
function birthday {
param([ref]$age)
$age.value += 1
}
birthday -age ([ref]$age)
Write-Output $age
I've got an age of 12. I pass it into a function as a parameter. The function increments the value of $age by 1. You can do the same thing with a function in a module. You get my drift.
I am trying to create an OCI instance with the Java SDK. I am getting an error in routeRules.
When I commented the following line, I was able to create an instance. But in that machines route is not enabled.
addInternetGatewayToRouteTable(vcnClient, compartmentId, vcn.getDefaultRouteTableId(), internetGateway);
https://github.com/oracle/oci-java-sdk/blob/master/bmc-examples/src/main/java/CreateInstanceExample.java
Exception in thread "main" com.oracle.bmc.model.BmcException: (400, InvalidParameter, false) routeRules[0].networkEntityId may not be null (opc-request-id: 6BC8A182852240F8AFFD1EB279CFF901/AD5BF82603D64DA298976FCFE83871F6/9398B04078B0435A8FA68FEA7307CC99)
at com.oracle.bmc.http.internal.ResponseHelper.throwIfNotSuccessful(ResponseHelper.java:120)
at com.oracle.bmc.http.internal.ResponseConversionFunctionFactory$ValidatingParseResponseFunction.apply(ResponseConversionFunctionFactory.java:86)
at com.oracle.bmc.http.internal.ResponseConversionFunctionFactory$ValidatingParseResponseFunction.apply(ResponseConversionFunctionFactory.java:82)
at com.oracle.bmc.core.internal.http.UpdateRouteTableConverter$1.apply(UpdateRouteTableConverter.java:70)
at com.oracle.bmc.core.internal.http.UpdateRouteTableConverter$1.apply(UpdateRouteTableConverter.java:55)
at com.oracle.bmc.core.VirtualNetworkClient.updateRouteTable(VirtualNetworkClient.java:3325)
at CreateInstanceExample.addInternetGatewayToRouteTable(CreateInstanceExample.java:295)
at CreateInstanceExample.main(CreateInstanceExample.java:146)
There is a bug in the code, you can replace destination in the addInternetGatewayToRouteTable(..) method
RouteRule internetAccessRoute =
RouteRule.builder()
.cidrBlock("0.0.0.0/0")
.destination(internetGateway.getId())
.build();
in line 295 with networkEntityId:
RouteRule internetAccessRoute =
RouteRule.builder()
.cidrBlock("0.0.0.0/0")
.networkEntityId(internetGateway.getId())
.build();
You can see from the REST APIs here which parameters are required to create a route rule in a route table.
The example seems to be based on a deprecated version of calling the RouteRule builder.
I'm creating instance on the Google Compute Engine with jclouds, using the command:
Template template = context.getComputeService().templateBuilder().imageId("debian-7-wheezy-v20150710").build();
Set<? extends NodeMetadata> listNode = context.getComputeService().createNodesInGroup("teste", 1, template);
But is the error below:
2015-08-16 07:27:04 INFO compute:64 - Image debian-7-wheezy-v20150710 not found in the image cache. Trying to get it from the provider...
Exception in thread "main" java.util.NoSuchElementException: imageId(debian-7-wheezy-v20150710) not found
at org.jclouds.compute.domain.internal.TemplateBuilderImpl.throwNoSuchElementExceptionAfterLoggingImageIds(TemplateBuilderImpl.java:764)
at org.jclouds.compute.domain.internal.TemplateBuilderImpl.findImageWithId(TemplateBuilderImpl.java:745)
at org.jclouds.compute.domain.internal.TemplateBuilderImpl.build(TemplateBuilderImpl.java:688)
at br.com.clouddeploy.main.TestGoogle.main(TestGoogle.java:47)
Any suggestion?
If you are using imageId() you probably need to use the full path to the image. Here's how to find it:
% gcloud compute images list --uri | grep debian-7
shows:
https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/backports-debian-7-wheezy-v20150710
https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20150710
So you can use either of these. The one that starts with backports has newer versions of packages, but is still based on Debian 7 Wheezy.
Alternatively, it looks like jclouds has the ability to do this URL creation on its own, but instead of using imageId(), you should use (see GitHub code example):
ComputeService compute = initComputeService(account, credentials);
[...]
TemplateBuilder templateBuilder = compute.templateBuilder();
templateBuilder.fromImage(compute
.getImage("debian-7-wheezy-v20140408"));
The LUA Developer's Guide ( part of Netezza Analytics 3.0 ) has instructions for "compiling" .nzl functions using the "nzlua" command. Unfortunately, the function seems to be only available in the DB that is defined in the environmental variable NZ_DATABASE when the "nzlua" command is executed - see command output immediately below.
Question: How does one make the compiled function available to all databases on the appliance ? i.e. without altering the NZ_DATABASE env variable and redoing the nzlua command for each DB ( including user sandbox DBs ).
[nz#nzh1p01 examples]$ /nz/extensions/nz/nzlua/bin/nzl nzlua isdate.nzl
Compiling: isdate.nzl
####################################################################
UdxName = isdate
UdxType = UDF
Arguments = VARCHAR(40),VARCHAR(40)
Result = BOOL
Dependencies = INZA.INZA.LIBNZLUA_3_0_0
NZUDXCOMPILE OPTIONS: (--nullcall --unfenced --mem 2m)
CREATE FUNCTION
The function should available in all the databases however you will need to call it using the full path. Database..function
Add the database that you registered the function into to the "search_path" environment variable in the /nz/data/postgresql.conf file. The function can then be reference d from anywhere :-)