|Linux| eBPF – BCC config and test

BCC := BPF Compiler Collection (ref. https://lwn.net/Articles/742082/ )

Originally, BPF is a tool for analyzing network traffic. After years, eBPF has been improved to be used for non-networking purposes, such as for attaching eBPF programs to various tracepoints. wiki

Comparing with XDP (driver layer tool for network, XDP test), BCC provides a tool chain for high level languages.
With this tool, developers can implement their own analyzing tools to manage BPF tools by Python, Lua or other high level languages.

This article will show how to config and run a test code.

|Python| Open a new console by sub-process

How can we open a new console to do another thing at the same time?
(multi-thread programming)
Ans: use “subprocess

def openNewConsole():
    '''open new file in Host consol'''
    log = open("myLogFile.log", "w")
    '''Create a subprocess to do "adb logcat" and let stdout send to "log"'''
    ret = subprocess.Popen("adb logcat -v threadtime -b all", creationflags =subprocess.CREATE_NEW_CONSOLE, stdout=log)
    return ret

|Python| cvs json

From .csv to .json
The format of data.csv:
country, location, school_name,url

I save every row as a dictionary “row = {}”
And “data” is the list of dictionaries.

import json
import csv

data = []
with open('data.csv', newline='') as csvFile:
csvReader = csv.DictReader(csvFile)
for each in csvReader:
 row ={}
 row['country'] = each['country']
 row['location'] = each['location']
 row['name'] = each['name']
 row['url'] = each['url']
 data.append(row)

jsonFilePath = 'saved.json'
with open(jsonFilePath, 'w') as write_file:
 write_file.write(json.dumps(data, indent = 4))

From .json to data structures

The format of saved.json:
// saved.json
[
 {
  ”country”: “US”,
  ”location”: “TX”,
  ”school”: “University of Texas”,
  ”url”: “https://ece.utdallas.edu/”
 },
 {
  ”country”: “US”,
  ”location”: “CA”,
  ”name”: “University of California”,
  ”url”: “”
 }
]

I put the array as dictionary.
Hence, I pull every array of json_array, then put into tmp_dict.

import json

with open('saved.json', 'r') as input_file:
    json_array = json.load(input_file)

store_dict_as_list = []
for item in json_array:
    tmp_dict = {}
    tmp_dict['country'] = item['country']
    tmp_dict['location'] = item['location']
    tmp_dict['name'] = item['name']
    tmp_dict['url'] = item['url']
    store_dict_as_list.append(tmp_dict)

|Python| Make your Line Bot on Heroku

Making Line bot is very simple.
There are 3 parts.
1. Apply Line Message API
2. Apply Google sheet API (This is not necessary. )
3. Deploy your service on Heroku

I design a robot. Every user can ask some information.
Those information were collected by everyone and wrote on online google sheet.

My Demo LineBot app


There are some Heroku usage:
// After install CIL tool…
heroku login
// If you need to see the log of backend
heroku logs
// If you want to run commands on Heroku
heroku run {some cmd you want to use in backend}
e.g.
heroku run ls
//If you want to do some regular job
https://devcenter.heroku.com/articles/scheduler

Notice:
You can NOT save file as running schedule job.
Heroku is IMMUTABLE.
You can not save file by your process.
 

References:
1. for Line:
https://github.com/yaoandy107/line-bot-tutorial
Line business
2. for Google:
如何透過python建立google表單-使用google-sheet-api
Google console
3. deploy on Heroku
https://github.com/yaoandy107/line-bot-tutorial