SoFunction
Updated on 2024-12-19

Python+influxdb+shell to write regional network status table

In this article, we share python+influxdb+shell to write a regional network status table for your reference, the details are as follows

Shell scripting section:

ex: is to ping each destination ip 10 packets, and then get the packet loss rate and average delay time, on Linux set as a timed task, every minute to execute. He will write the data to the top of the influxdb server, denoting hk_vnloss and hk_vn01rtt, the fields are loss and rtt, and all other regions are similar.

python code section:

1. Inside the Django project's setting, configure the influxdb database connection;

ALLOWED_HOSTS = ['*']

# Application definition

INSTALLED_APPS = [
  '',
  '',
  '',
  '',
  '',
  '',
  'network_ping.',
  'influxdb_metrics',
]

##influxdb
INFLUXDB_HOST = 'influxdb server ip'
INFLUXDB_PORT = 'influxdb port'
INFLUXDB_USER = 'influxdb username'
INFLUXDB_PASSWORD = 'influxdb passwd'
INFLUXDB_DATABASE = 'influxdb database'
INFLUXDB_TIMEOUT = 36000

2. Query the saved loss and rtt data from the influxdb database;

from influxdb_metrics.utils import query


def get_loss(tab_name):
  loss_res = query('SELECT * FROM %s ORDER BY time DESC limit 1' % tab_name)
  loss = [res for res in loss_res]
  return loss

3. Pass the data to the web page;

##network ping loss
def network_ping(request):
  # vn01 ping vn01
  vnloss01 = get_loss('vnloss')
  # vn01 ping vn02
  vnloss02 = get_loss('vn02loss')
  # vn01 ping th
  thloss = get_loss('thloss')
  # vn01 ping id
  idloss = get_loss('idloss')
  # vn01 ping sg
  sgloss = get_loss('sgloss')
  # vn01 ping tw
  twloss = get_loss('twloss')
  # vn01 ping hk
  hkloss = get_loss('hkloss')
  # vn01 ping sh
  shloss = get_loss('shloss')
  return render(request, 'net_ping.html',
         {'vn01': {
           'vnloss01': vnloss01,
           'vnloss02': vnloss02,
           'thloss': thloss,
           'hkloss': hkloss,
           'idloss': idloss,
           'sgloss': sgloss,
           'twloss': twloss,
           'shloss': shloss
         },...)

4. Get to display it on the web page;

<tr>
  <td bgcolor="#f0f8ff" class="titlestyle">VN-01<br/>45.119.241.249</td>
  <td tloss={{ vn01.vnloss01.0. }}></td>
  <td tloss={{ vn01.vnloss02.0. }}></td>
  <td tloss={{ .0. }}></td>
  <td tloss={{ .0. }}></td>
  <td tloss={{ .0. }}></td>
  <td tloss={{ .0. }}></td>
  <td tloss={{ .0. }}></td>
  <td tloss={{ .0. }}></td>
</tr>

5. js timed page refresh;

$(function () {
    $("[tloss]").each(function () {
      var loss_res = $(this).attr('tloss');
      if (loss_res < 10) {
        $(this).html('<span class="greentyle"></span><br/>loss: <span bg>' + loss_res);
      } else if (loss_res <= 20) {
        $(this).html('<span class="yellowstyle"></span><br/>loss: ' + loss_res);
      } else {
        $(this).html('<span class="redstyle"></span><br/>loss: ' + loss_res);
      }
    });
    setTimeout("();", 60000);
  })

6, the final effect of the display;

Finally: the page is refreshed regularly every minute, but this is all the current ping loss and rtt values, to see the historical ping, you can build a Grafana, and then Grafana configure the data source to influxdb, then you can view the historical curves and so on, for example:

This is the entire content of this article.