Mini status Dashboard to switch WWAN links

The netmodule SDK allows to to add addtional web-pages to the status section of the webinterface. In this article, you will learn how to setup the dashboard sdk-script that provides the ability to switch between wanlinks and reboot the router.

The new added page will look like this:

Prerequierments

  • NRSW 4.6.0.105 or NRSW 4.8.0.102

Installation

If you dont have the required software version please install it first.

Adding the SDK script

Open the netmodule webinterface and navigate to:

Services → SDK → Job Management

Add a SDK trigger

In this case, we want to start the dashboard at the startup, so we use an event based trigger; sdk-startup

Add the SDK script

In the script tab copy and paste the sdk script.

dashboard.are
ID = 1;
hotlink = "";
 
 
string pp_var(var) {
 
    tmp=tolower(var);
    tmp=explode(tmp);
 
    tmp[0] = toupper(tmp[0]);
    for (i=0; i<length(tmp); i++) {
 
        if (tmp[i] == '_' ) {
            tmp[i] = ' ';
            tmp[i+1] = toupper(tmp[i+1]);
        }
    }
    return implode(tmp);
 
}
 
string pp(value) {
    switch (value) {
        case "up":         return '<font color="green">up</front>';
        case "good":       return '<font color="green">good</front>';
        case "excelent":   return '<font color="green">excelent</front>';
 
        case "dialing":    return '<font color="orange">dialing</font>';
        case "medium":     return '<font color="orange">medium</font>';
 
        case "weak":       return '<font color="red">weak</font>';
        case "down":       return '<font color="red">down</font>';
    }
 
    return value;
}
 
 
void print_table(array fields) {
     nb_page_respond(page, '<table>\n');
     nb_page_respond(page, '<tr><th><b>');
     nb_page_respond(page, left(fields[0], 8));
     nb_page_respond(page, '</b></th><th>');
 
     if (struct_get(wan, "WAN_HOTLINK") != left(fields[0], 8) ) {
        nb_page_respond(page, '<form action="sdkPage.php" type="GET" style="padding-top: 2px;margin-bottom: 2px;margin-top: 2px;right;text-align: right;">
                               <input type="hidden" id="id" name="id" value="00000001">');
        nb_page_respond(page,  sprintf("<input type='hidden' id='hotlink' name='hotlink' value='%s'>",left(fields[0], 8)));
        nb_page_respond(page, '<input style="margin-top: 0px" type="submit" value="Use as Hotlink"></form>');
     } 
 
     nb_page_respond(page, '</th></tr>');
 
         for (i=0; i < length(fields); i++) {
             nb_page_respond(page, '<tr>\n');
             nb_page_respond(page, '<td>%s</td>', sprintf("%s", pp_var(substr(fields[i], 9))));
             nb_page_respond(page, sprintf("<td align='right'>%s</td>", pp(struct_get(wan, fields[i]))));
             nb_page_respond(page, '</tr>\n');
         }
 
         nb_page_respond(page, '</table>\n');
         nb_page_respond(page, '<br>\n');
 
}
 
 
 
     /* register page */
     page = nb_userpage_register(ID, "Wanlink", "Switch");
     if (is_void(page)) {
         printf("unable to register page\n");
         exit(1);
     }
 
     while (1) {
 
        printf("waiting for requests...\n");
        /* wait for page request */
        request = nb_page_request(page);
 
        if (!is_void(request)) {
           /* evaluate GET params */
           GET = struct_get(request, "GET");
        }
        for (k = 0; k < length(GET); k++) {
            key = struct_get(GET[k], "key");
            if (key == "hotlink") {
                hotlink = struct_get(GET[k], "value");
            }
            if (key == "action") {
                action = struct_get(GET[k], "value");
                if (action == "reboot") 
                    nb_reboot(0);
            }
        }
        if (hotlink != "") {
            nb_wanlink_priorize(hotlink, 2);
            sleep(5);
        }
 
 
        fields = mkarray();
 
        wan = nb_status("wan");
        for (i=1; i <=9; i++) { 
 
            wan_state = struct_get(wan, sprintf("WANLINK%d_STATE", i));
            wan_idx =  sprintf("WANLINK%d", i);
 
            if (strlen(wan_state) > 0) {
 
                // Attributes for WWAN link
                wan_array = mkarray(sprintf("%s_STATE", wan_idx), 
                                    sprintf("%s_TYPE", wan_idx), 
                                    sprintf("%s_SIGNAL_LEVEL", wan_idx), 
                                    sprintf("%s_SIGNAL_QUALITY", wan_idx), 
                                    sprintf("%s_SERVICE_TYPE", wan_idx), 
                                    sprintf("%s_NETWORK", wan_idx)
                                    );
 
                // Attributes for ETH/LAN link
                eth_array = mkarray(sprintf("%s_STATE", wan_idx), 
                                    sprintf("%s_TYPE", wan_idx), 
                                    sprintf("%s_ADDRESS", wan_idx), 
                                    sprintf("%s_GATEWAY", wan_idx)
                                    );
 
 
                type = struct_get(wan, sprintf("WANLINK%d_TYPE", i));
                switch (type) {
                    case "eth": tmp_array = eth_array;
                            break;
                    case "wwan": tmp_array = wan_array;
                            break;
                }
                fields[i-1] = tmp_array;
            }
        }
 
        nb_page_respond(page, '<table>\n');
        nb_page_respond(page, '<tr>\n');
        nb_page_respond(page, '<td>Hotlink</td>');
 
        nb_page_respond(page, sprintf("<td align='right'>%s</td>", struct_get(wan, "WAN_HOTLINK")));
        nb_page_respond(page, '</tr></table>\n');
        nb_page_respond(page, '<br>\n');
 
        nb_page_respond(page, '<table>\n');
        nb_page_respond(page,'<tr><td>Reebot</td><td align="right">
                             <form action="sdkPage.php" type="GET">
                             <input type="hidden" id="id" name="id" value="00000001">
                             <input type="hidden" id="action" name="action" value="reboot">
                             <input style="margin-top: 0px" type="submit" value="reboot"></td></tr></form>\n');
 
        nb_page_respond(page, '</tr></table>\n');
        nb_page_respond(page, '<br>\n');
 
 
        for (j=0; j<length(fields); j++) {
            if (!is_void(fields[j])) {
                print_table(fields[j]);
            }
        }
 
        nb_page_finish(page);
  }
 
nb_page_unregister(page);
exit(0);

Create a SDK job

Once we have the trigger and the script, we can create job that will be exected.

Enable netmodule SDK

The last step is to enable the sdk. SERVICES → SDK → Administation

Check your installation

After the sdk has been started, you should have a new status page.

Navigate to HOME → Overwiev → Interfaces

The page might look diffrent, depending on your number of wanlinks.

Your current hotlink is displayed at the top. Also, each enabled wanlink will be listet on the new status page. All links, except for the current hotlink, have a button “Use as hotlink”. This can be used to change the current hotlink. This may take some time, sometimes the hotlink will be shown as “n/a”. Reload the page and the new hotlink will be displayed.

Reboot the Router

The page has a reboot button. The device will reboot after 5 seconds.

You can add/remove the fields that are shown.

                 // Attributes for WWAN link
                 wan_array = mkarray(sprintf("%s_STATE", wan_idx),
                                     sprintf("%s_TYPE", wan_idx),
                                     sprintf("%s_SIGNAL_LEVEL", wan_idx),
                                     sprintf("%s_SIGNAL_QUALITY", wan_idx),
                                     sprintf("%s_SERVICE_TYPE", wan_idx),
                                     sprintf("%s_NETWORK", wan_idx)
                                     );
 
                 // Attributes for ETH/LAN link
                 eth_array = mkarray(sprintf("%s_STATE", wan_idx),
                                     sprintf("%s_TYPE", wan_idx),
                                     sprintf("%s_ADDRESS", wan_idx),
                                     sprintf("%s_GATEWAY", wan_idx)
                                     );

For all attributes, use the shell command “cli status -s wan”:

~ $ cli status -s wan
WAN_HOTLINK="WANLINK1"
WANLINK1_STATE="up"
WANLINK1_STATE_IPV4="up"
WANLINK1_STATE__SINCE="2023-10-30 13:59:16"
WANLINK1_TYPE="eth"
WANLINK1_INTERFACE="LAN1-1"
WANLINK1_ADDRESS="10.0.0.189"
WANLINK1_GATEWAY="10.0.0.1"
WANLINK1_ADDRESS_IPV6="n/a"
WANLINK1_DIAL_ATTEMPTS="1"
WANLINK1_DIAL_SUCCESS="1"
WANLINK1_DIAL_FAILURES="0"
WANLINK1_DATA_DOWNLOADED="69869"
WANLINK1_DATA_UPLOADED="63414"
WANLINK1_DATA_SINCE="2023-10-30 10:41:50"
WANLINK1_DOWNLOAD_RATE="0"
WANLINK1_UPLOAD_RATE="0"
WANLINK2_STATE="down"
WANLINK2_STATE__SINCE="startup"
WANLINK2_TYPE="wwan"
WANLINK2_INTERFACE="WWAN1"
WANLINK2_ADDRESS="n/a"
WANLINK2_ADDRESS_IPV6="n/a"
WANLINK2_MODEM="Mobile1"
WANLINK2_SIM="SIM2"
WANLINK2_PDP="PDP1"
WANLINK2_SIGNAL_STRENGTH="-103"
WANLINK2_SIGNAL_LEVEL="16"
WANLINK2_SIGNAL_QUALITY="bad"
WANLINK2_REGISTRATION_STATE="searchingNetwork"
WANLINK2_SERVICE_TYPE="EDGE"
WANLINK2_NETWORK="n/a"
WANLINK2_DIAL_ATTEMPTS="0"
WANLINK2_DIAL_SUCCESS="0"
WANLINK2_DIAL_FAILURES="0"
WANLINK2_DATA_DOWNLOADED="n/a"
WANLINK2_DATA_UPLOADED="n/a"
WANLINK2_DATA_SINCE="2023-10-30 10:43:00"
WANLINK2_DOWNLOAD_RATE="0"
WANLINK2_UPLOAD_RATE="0"

Thats it. Enjoy your new status page.