This post will show you how to solve ‘BPF’ object has no attribute ‘get_syscall_fnname’ when run bpf program in linux ?
Problem
When you run a python bpf_program in linux, you run this command:
For example, if our bpf program’s name is example.py:
The example.py content is:
You get this error:
The error AttributeError: 'BPF' object has no attribute 'get_syscall_fnname' indicates that the BPF class from the bcc module does not have a method named get_syscall_fnname.
Environment
You check your os version by this command:
Python version:
Solution: Install the bcc dependencies
According to python bcc documents, you should install the libbcc and python bcc into system.
what is libbcc?
and what is python-bcc:
After all done, you can run the python bpf script again:
It works!
Final Words + More Resources
My intention with this article was to help others who might be considering solving such problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
In this post, I will demo how to create a python script to check a url(website/webservice)‘s health periodically and automatically .
2. Environment
Python 2.x or 3.x
Linux system
3. The solution
3.1 The environment
Suppose we hava a web service which provides some sort of services to users, now we want it to run always and if it’s not healthy, we should restart the service automatically.
The service URL prefix is:
And we also have a restart script for this service located at:
3.2 Add a health check endpoint for our service
For health check purpose, we should add a dedicated endpoint to be accessed by the health check script, it should be fast , here is our implementation in java language:
It means that if we caccess the below url:
We should get this result(http response status code equals 200):
Otherwise , the status code should not be 200 , just as the picture shows:
It health check fails, the restart script should be run immediately.
3.3 Setup the logger and imports for python script
Before coding the business, we should setup the environment for our script, just as follows:
In the above code, we use the logging module in python to setup a daily rolling file handler for our program, it would create a log file everyday with the suffix year-month-day. You can view more information about the TimedRotatingFileHandler from this website.
The parameter when="midnight" in TimedRotatingFileHandler means that it would Roll over at midnight.
3.4 Implement the health check function with python
First, we need a function to check the status of the service, just as follows:
You can see that , the above function would return True only when the service response code is 200 and the responsecontent is world, or else, it would return False to the caller.
For python 2.x , we can use:
For python 3.x , you can replace the urllib.urlopen with follows:
3.5 Implement the periodically checking logic
Now we should create a function to run the health check periodically and restart the service automatically if the check fails.
3.6 The main function
At last, we should provide the main function to execute the whole script:
3.7 Save the script and execute it in background
Then we save the above script as healthcheck_myservice.py, and execute in background as follows:
Then we can check the log as follows:
We can get these logs:
It works!
4. The whole script
5. Summary
In this post, I demonstrated how to use python to do health check job in linux.
In this post, I will demonstrate how to copy file using python shutils , and will also show you how to copy without overwriting the destination file.
2. The Environment
Python 3
Shutils
3. The code
3.1 How to copy file?
This is the demo that shows how to copy file from src to dest:
3.2 Test the copy function
Suppose our working directory structure as follows:
Now I want to copy logo.png to images directory, and name it logo_bak.png, I do this job as follows:
After run the above command, I get this directory structure:
3.3 How to copy without overwritten
The code:
3.4 Test the copy again
Our directory is :
Then I execute the below command in the working directory:
It works!
4. About the shutil
Shutil module in Python provides many functions of high-level operations on files and collections of files. … This module helps in automating process of copying and removal of files and directories. shutil. copy() method in Python is used to copy the content of source file to destination file or directory.
Shutil is the abbreviation of shell utility, which implements advanced functions such as file copying, moving, compression, and decompression in Python. It is a Python system module and does not require additional installation.
The commonly used functions in shutil are listed below:
shutil.copyfile(src, dst) Copy from source src to dst. Of course, the premise is that the target address has writable permissions. The exception information thrown is IOException. If the current dst already exists, it will be overwritten
shutil.move(src, dst) move file or rename
shutil.copymode(src, dst) just copy its permissions and other things will not be copied
shutil.copystat(src, dst) copy permission, last access time, last modification time
shutil.copy(src, dst) copy a file to a file or a directory
shutil.copy2(src, dst) is copied on the basis of copy and the last access time and modification time of the file are also copied, similar to cp -p
shutil.copy2(src, dst) If the file systems in the two locations are the same, it is equivalent to a rename operation, just rename; if it is not in the same file system, it is a move operation
shutil.copytree(olddir, newdir, True/Flase) copies olddir to newdir. If the third parameter is True, the symbolic link under the folder will be kept when copying the directory. If the third parameter is False, it will Generate a physical copy in the copied directory to replace the symbolic link
shutil.rmtree(src) recursively delete a directory and all contents in the directory
5. Summary
In this post, I demonstrated how to use shutil in python to copy files from here to there. And I also demonstrated how to avoid overwriting the dest file when copying. Thanks for your reading. Regards.