Switching

 View Only
last person joined: 4 days ago 

Ask questions and share experiences about EX and QFX portfolios and all switching solutions across your data center, campus, and branch locations.
Expand all | Collapse all

How can a switch get trigger events from an external device like a UPS?

  • 1.  How can a switch get trigger events from an external device like a UPS?

    Posted 07-22-2024 13:28

    We would like to be able to have our switches react to events on our UPSes. Example:" If UPS has less than 5 min of battery left, power down the switch. " We would have other commands for other UPS events.  I just don't know how to pass an event into the switch to evaluate and run a CLI command based on it. 



    ------------------------------
    JOHN WILLIAMSON
    ------------------------------


  • 2.  RE: How can a switch get trigger events from an external device like a UPS?

     
    Posted 07-24-2024 09:24
    Edited by asharp 07-25-2024 04:18

    Hi,

    I'm not sure that Junos provides the capabilities to receive an event in that manner.  Or maybe it does and I'm not familiar with that functionality...?

    I guess other options would depend on what the capabilities are for triggering events from the UPS.  For example, perhaps it is possible to trigger a command to execute using SSH that in conjunction with NETCONF and using ssh certificates you could instruct the device to shutdown.

    e.g.

    ssh -i ~/.ssh/id_rsa auto@192.168.0.212 -s netconf < shutdown_rpc.txt
    
    more shutdown_rpc.txt
    <rpc><request-power-off><in>3</in></request-power-off></rpc>
    <rpc><close-session/></rpc>
    
    This will shutdown the box in 3 minutes as an example.

    Regards,



    ------------------------------
    Andy Sharp
    ------------------------------



  • 3.  RE: How can a switch get trigger events from an external device like a UPS?

    Posted 07-24-2024 10:10

    I'll be sending the alerts from Solarwinds which we use to monitor the UPSes and other devices.  Is there a way to use Postman and REST API to talk to the switches? 



    ------------------------------
    JOHN WILLIAMSON
    ------------------------------



  • 4.  RE: How can a switch get trigger events from an external device like a UPS?

     
    Posted 07-24-2024 14:36

    I'm not familiar with Postman, but with curl you would expect to do something like the following:

    I haven't verified it as I don't have access to a device right now that supports the REST API.  But I would hope something like the following would work.  Either using a POST with text/plain or with xml data.

    curl -u "username:password" http://device-name:port/rpc/request-power-off --header "Content-Type: text/plain" –d "in=3"
    curl -u "username:password" http://device-name:port/rpc/request-power-off --header "Content-Type: application/xml" –d "<in>3</in>"

    Of course it depends on the version of Junos deployed and if REST is enabled in the configuration etc.

    Regards,



    ------------------------------
    Andy Sharp
    ------------------------------



  • 5.  RE: How can a switch get trigger events from an external device like a UPS?

    Posted 07-24-2024 17:32

    Is there a list of EX switch rpc commands somewhere? I can't find it. I'd like to know what I can get and post to help figure out what I can tell the switch to do.



    ------------------------------
    JOHN WILLIAMSON
    ------------------------------



  • 6.  RE: How can a switch get trigger events from an external device like a UPS?

     
    Posted 07-24-2024 19:20
    Edited by asharp 07-24-2024 19:26

    Hi,

    Within Junos itself, any CLI command can also provide you with what the RPC is associated with the command that you are trying to find.

    Just add, "| display xml rpc" to the end of the command that you want to check.  If there is an RPC available specific to that command, it will be displayed.

    > request system power-off in 5 | display xml rpc
    <rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.1X46/junos">
        <rpc>
            <request-power-off>
                    <in>5</in>
            </request-power-off>
        </rpc>
        <cli>
            <banner>{primary:node1}</banner>
        </cli>
    </rpc-reply>

    Another example.

    > show chassis hardware extensive | display xml rpc
    <rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.1X46/junos">
        <rpc>
            <get-chassis-inventory>
                    <extensive/>
            </get-chassis-inventory>
        </rpc>
        <cli>
            <banner>{primary:node1}</banner>
        </cli>
    </rpc-reply>

    There are some commands that do not have any RPC associated with them, for example "show chassis fan | display xml rpc" will probably return the response "xml rpc equivalent of this command is not available.", if you are running an old Junos release, newer releases support the RPC <get-fan-information> as far as I can recall.  However, you can still make an RPC to perform that task, you just have to use the RPC command, which allows you to execute normal cli commands via an RPC. e.g.

    <rpc><command>show chassis fan</command></rpc>

    Some reference information on the subject can be found here: https://www.juniper.net/documentation/us/en/software/junos/netconf/junos-xml-protocol/topics/task/junos-xml-protocol-rpcs-and-xml-mapping.html#mapping-commands-to-junos-xml-request-tag-elements

    XML API Explorer: https://apps.juniper.net/xmlapi/operational/

    With the XML API Explorer, you can enter the cli command in the filter field and that will also return the RPC equivalent where available for the particular Junos version you are interested in.

    I rarely use it, most times I'm just running the "| display xml rpc" command via the CLI to find out the available RPC, but it is there if you need to reference it.

    Regards,



    ------------------------------
    Andy Sharp
    ------------------------------



  • 7.  RE: How can a switch get trigger events from an external device like a UPS?

    Posted 07-25-2024 10:33

    I found the reference to that "display XML rpc" but I expected a doc rather than just trying each command and hoping it was supported. 



    ------------------------------
    JOHN WILLIAMSON
    ------------------------------



  • 8.  RE: How can a switch get trigger events from an external device like a UPS?

    Posted 07-25-2024 13:02

    I'm using the Rest API  Explorer that I have enabled on a test switch.

    I was able to test the power off command, and that worked. Very good. Now, I want to test a command that won't requirement to wait for this EX 2300-12 to reboot each time. I would like to send a simple command that would do something simple, like show version which = get-software-information in rpc.  I'd like to see it happen in the switch CLI so I know it worked.   I thought that would be a post and then I would but the rpc code in , like this:

    <rpc>

    <get-software-information>

    </get-software-information>

    </rpc>

    But that does not show any action on the switch in the CLI as a confirmation.  I've tried both a simple get and a post and have the same result. 



    ------------------------------
    JOHN WILLIAMSON
    ------------------------------



  • 9.  RE: How can a switch get trigger events from an external device like a UPS?

     
    Posted 07-26-2024 08:44
    Edited by Juniper Community Admin 07-26-2024 08:43

    Okay, so the "| display xml rpc" only returns the RPC information for that particular command, it doesn't actually execute the command.

    If you want to see what the data returned is like within the CLI, then just omit the "rpc" from the command. e.g.

    show version | display xml | no-more
    <rpc-reply xmlns:junos="http://xml.juniper.net/junos/22.1R0/junos">
        <software-information>
            <host-name>test960</host-name>
            <product-model>mx960</product-model>
            <product-name>mx960</product-name>
            <junos-version>22.1R1.10</junos-version>
            ....
        </software-information>
        <cli>
            <banner></banner>
        </cli>
    </rpc-reply>

    That will execute the given command (show version) in this example, and return the data in the format specified, XML in this instance. 

    Please note that the command is being executed, just that using the "| display xml" instructs MGD to return the data in its native XML format, rather than the rendered CLI version.

    You could also substitute XML for JSON and get the data returned in that format.  e.g. show version | display json

    show version | display json | no-more
    {
        "software-information" : [
        {
            "host-name" : [
            {
                "data" : "test960"
            }
            ],
            "product-model" : [
            {
                "data" : "mx960"
            }
            ],
            "product-name" : [
            {
                "data" : "mx960"
            }
            ],
            "junos-version" : [
            {
                "data" : "22.1R1.10"
            }
    
    etc.

    If you wanted to make an RPC and return the data in a particular format (set, text, json, xml) taking into consideration that set format is only valid for configuration rather than show/request commands then it would look something like the following:  In this example, I am using SSH to connect to the device, but using the -s netconf argument to SSH so that I can access the netconf subsytem on the router.  Then I just enter the RPC directly and hit Enter to see the rpc reply from the device.

    $ ssh test960 -s netconf
    ....
    <rpc><get-system-information format="text"/></rpc>
    <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/22.1R0/junos">
    <output>
    Model: mx960
    Family: junos
    Junos: 22.1R1.10
    Hostname: test960
    </output>
    </rpc-reply>
    
    <rpc><get-system-information format="json"/></rpc>
    <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/22.1R0/junos">
    {
        "system-information" : [
        {
            "hardware-model" : [
            {
                "data" : "mx960"
            }
            ],
            "os-name" : [
            {
                "data" : "junos"
            }
            ],
            "os-version" : [
            {
                "data" : "22.1R1.10"
            }
            ],
            "host-name" : [
            {
                "data" : "test960"
            }
            ]
        }
        ]
    }
    </rpc-reply>
    
    <rpc><close-session/></rpc>
    
    <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/22.1R0/junos">
    <ok/>
    </rpc-reply>
    ]]>]]>
    <!-- session end at 2024-07-25 17:26:54 PDT -->

    Or of course continue to use curl to perform the same.

    curl -u "username:password" http://xxxxx:xxxx/rpc/get-system-information@format=json
    
    curl -u "username:password" http://xxxx:xxxx/rpc/get-system-information -H "Accept: application/json"

    Regards,



    ------------------------------
    Andy Sharp
    ------------------------------



  • 10.  RE: How can a switch get trigger events from an external device like a UPS?

    Posted 07-26-2024 10:29

    Andy,

    I'm not so much concerned about the format it returns, but that I can send a rpc command and the switch does respond. So, I would test my alerts with a boring command before changing them to power off commands. 



    ------------------------------
    JOHN WILLIAMSON
    ------------------------------



  • 11.  RE: How can a switch get trigger events from an external device like a UPS?

     
    Posted 07-26-2024 12:08
    Edited by asharp 07-26-2024 13:26

    Okay, so choose any of the options available.

    All of these approaches perform the given command and will respond with a NETCONF <reply>.

    NETCONF can also be called via numerous different scripting languages too, Python, Ruby, Perl, SLAX etc. They all return the data in a similar manner, e.g the contents of the NETCONF reply in XML.

    I also tested with Postman, and again had no issue with making a POST to the box to obtain data.

    POST https://<device_ip>:3443/rpc
    Authorization: Basic Auth
    Headers:
    Content-Type: application/xml
    Accept: application/xml
    Body:
    <request-system-storage-cleanup>
    <dry-run/>
    </request-system-storage-cleanup>
    
    This returned data as I would expect:
    --harqgehabymwiax
    Content-Type: application/xml; charset=utf-8
    <system-storage-cleanup-information xmlns:junos="http://xml.juniper.net/junos/*/junos" format="xml">
        <file-list junos:style="normal">
            <file>
                <file-name>/var/log/default-log-messages.0.gz</file-name>
                <size junos:format="134B">134</size>
                <date>Jul 26 10:13</date>
            </file>
        </file-list>
        <success/>
    </system-storage-cleanup-information>
    --harqgehabymwiax--

     

    Regards,



    ------------------------------
    Andy Sharp
    ------------------------------



  • 12.  RE: How can a switch get trigger events from an external device like a UPS?

    Posted 07-25-2024 11:57

    With curl, is the username and password in clear text when the command is issued?  



    ------------------------------
    JOHN WILLIAMSON
    ------------------------------



  • 13.  RE: How can a switch get trigger events from an external device like a UPS?

     
    Posted 07-25-2024 12:53

    I would expect so, unless using HTTP/S.  I've not tried it myself, but HTTP/S would be the correct approach.

    Regards,



    ------------------------------
    Andy Sharp
    ------------------------------



  • 14.  RE: How can a switch get trigger events from an external device like a UPS?

     
    Posted 07-25-2024 13:01

    If you want to see all commands for a particular version of Junos, then use the XML API Explorer instead.

    But honestly, I can't recall the last time that I encountered a command that I needed to use that wasn't supported via an RPC directly, or indirectly using the RPC <command>....</command> approach.   I have encountered more situations where a particular cli command wasn't available because of an older version of Junos being used and not supporting that particular functionality.

    Regards,



    ------------------------------
    Andy Sharp
    ------------------------------