mirror of
https://github.com/LCTT/TranslateProject.git
synced 2026-08-23 04:03:29 +08:00
Merge remote-tracking branch 'LCTT/master'
This commit is contained in:
@@ -0,0 +1,218 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (An-DJ)
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: (wxy)
|
||||
[#]: url: (https://linux.cn/article-10595-1.html)
|
||||
[#]: subject: (How To Check CPU, Memory And Swap Utilization Percentage In Linux?)
|
||||
[#]: via: (https://www.2daygeek.com/linux-check-cpu-memory-swap-utilization-percentage/)
|
||||
[#]: author: (Vinoth Kumar https://www.2daygeek.com/author/vinoth/)
|
||||
|
||||
如何查看 Linux 下 CPU、内存和交换分区的占用率?
|
||||
======
|
||||
|
||||
在 Linux 下有很多可以用来查看内存占用情况的命令和选项,但是我并没有看见关于内存占用率的更多的信息。
|
||||
|
||||
在大多数情况下我们只想查看内存使用情况,并没有考虑占用的百分比究竟是多少。如果你想要了解这些信息,那你看这篇文章就对了。我们将会详细地在这里帮助你解决这个问题。
|
||||
|
||||
这篇教程将会帮助你在面对 Linux 服务器下频繁的内存高占用情况时,确定内存使用情况。
|
||||
|
||||
而在同时,如果你使用的是 `free -m` 或者 `free -g`,占用情况描述地也并不是十分清楚。
|
||||
|
||||
这些格式化命令属于 Linux 高级命令。它将会对 Linux 专家和中等水平 Linux 使用者非常有用。
|
||||
|
||||
### 方法-1:如何查看 Linux 下内存占用率?
|
||||
|
||||
我们可以使用下面命令的组合来达到此目的。在该方法中,我们使用的是 `free` 和 `awk` 命令的组合来获取内存占用率。
|
||||
|
||||
如果你正在寻找其他有关于内存的文章,你可以导航到如下链接。这些文章有 [free 命令][1]、[smem 命令][2]、[ps_mem 命令][3]、[vmstat 命令][4] 及 [查看物理内存大小的多种方式][5]。
|
||||
|
||||
要获取不包含百分比符号的内存占用率:
|
||||
|
||||
```
|
||||
$ free -t | awk 'NR == 2 {print "Current Memory Utilization is : " $3/$2*100}'
|
||||
或
|
||||
$ free -t | awk 'FNR == 2 {print "Current Memory Utilization is : " $3/$2*100}'
|
||||
|
||||
Current Memory Utilization is : 20.4194
|
||||
```
|
||||
|
||||
要获取不包含百分比符号的交换分区占用率:
|
||||
|
||||
```
|
||||
$ free -t | awk 'NR == 3 {print "Current Swap Utilization is : " $3/$2*100}'
|
||||
或
|
||||
$ free -t | awk 'FNR == 3 {print "Current Swap Utilization is : " $3/$2*100}'
|
||||
|
||||
Current Swap Utilization is : 0
|
||||
```
|
||||
|
||||
要获取包含百分比符号及保留两位小数的内存占用率:
|
||||
|
||||
```
|
||||
$ free -t | awk 'NR == 2 {printf("Current Memory Utilization is : %.2f%"), $3/$2*100}'
|
||||
或
|
||||
$ free -t | awk 'FNR == 2 {printf("Current Memory Utilization is : %.2f%"), $3/$2*100}'
|
||||
|
||||
Current Memory Utilization is : 20.42%
|
||||
```
|
||||
|
||||
要获取包含百分比符号及保留两位小数的交换分区占用率:
|
||||
|
||||
```
|
||||
$ free -t | awk 'NR == 3 {printf("Current Swap Utilization is : %.2f%"), $3/$2*100}'
|
||||
或
|
||||
$ free -t | awk 'FNR == 3 {printf("Current Swap Utilization is : %.2f%"), $3/$2*100}'
|
||||
|
||||
Current Swap Utilization is : 0.00%
|
||||
```
|
||||
|
||||
如果你正在寻找有关于交换分区的其他文章,你可以导航至如下链接。这些链接有 [使用 LVM(逻辑盘卷管理)创建和扩展交换分区][6],[创建或扩展交换分区的多种方式][7] 和 [创建/删除和挂载交换分区文件的多种方式][8]。
|
||||
|
||||
键入 `free` 命令会更好地作出阐释:
|
||||
|
||||
```
|
||||
$ free
|
||||
total used free shared buff/cache available
|
||||
Mem: 15867 3730 9868 1189 2269 10640
|
||||
Swap: 17454 0 17454
|
||||
Total: 33322 3730 27322
|
||||
```
|
||||
|
||||
细节如下:
|
||||
|
||||
* `free`:是一个标准命令,用于在 Linux 下查看内存使用情况。
|
||||
* `awk`:是一个专门用来做文本数据处理的强大命令。
|
||||
* `FNR == 2`:该命令给出了每一个输入文件的行数。其基本上用于挑选出给定的行(针对于这里,它选择的是行号为 2 的行)
|
||||
* `NR == 2`:该命令给出了处理的行总数。其基本上用于过滤给出的行(针对于这里,它选择的是行号为 2 的行)
|
||||
* `$3/$2*100`:该命令将列 3 除以列 2 并将结果乘以 100。
|
||||
* `printf`:该命令用于格式化和打印数据。
|
||||
* `%.2f%`:默认情况下,其打印小数点后保留 6 位的浮点数。使用后跟的格式来约束小数位。
|
||||
|
||||
### 方法-2:如何查看 Linux 下内存占用率?
|
||||
|
||||
我们可以使用下面命令的组合来达到此目的。在这种方法中,我们使用 `free`、`grep` 和 `awk` 命令的组合来获取内存占用率。
|
||||
|
||||
要获取不包含百分比符号的内存占用率:
|
||||
|
||||
```
|
||||
$ free -t | grep Mem | awk '{print "Current Memory Utilization is : " $3/$2*100}'
|
||||
Current Memory Utilization is : 20.4228
|
||||
```
|
||||
|
||||
要获取不包含百分比符号的交换分区占用率:
|
||||
|
||||
```
|
||||
$ free -t | grep Swap | awk '{print "Current Swap Utilization is : " $3/$2*100}'
|
||||
Current Swap Utilization is : 0
|
||||
```
|
||||
|
||||
要获取包含百分比符号及保留两位小数的内存占用率:
|
||||
|
||||
```
|
||||
$ free -t | grep Mem | awk '{printf("Current Memory Utilization is : %.2f%"), $3/$2*100}'
|
||||
Current Memory Utilization is : 20.43%
|
||||
```
|
||||
|
||||
要获取包含百分比符号及保留两位小数的交换空间占用率:
|
||||
|
||||
```
|
||||
$ free -t | grep Swap | awk '{printf("Current Swap Utilization is : %.2f%"), $3/$2*100}'
|
||||
Current Swap Utilization is : 0.00%
|
||||
```
|
||||
|
||||
### 方法-1:如何查看 Linux 下 CPU 的占用率?
|
||||
|
||||
我们可以使用如下命令的组合来达到此目的。在这种方法中,我们使用 `top`、`print` 和 `awk` 命令的组合来获取 CPU 的占用率。
|
||||
|
||||
如果你正在寻找其他有关于 CPU(LCTT 译注:原文误为 memory)的文章,你可以导航至如下链接。这些文章有 [top 命令][9]、[htop 命令][10]、[atop 命令][11] 及 [Glances 命令][12]。
|
||||
|
||||
如果在输出中展示的是多个 CPU 的情况,那么你需要使用下面的方法。
|
||||
|
||||
```
|
||||
$ top -b -n1 | grep ^%Cpu
|
||||
%Cpu0 : 5.3 us, 0.0 sy, 0.0 ni, 94.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
|
||||
%Cpu1 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
|
||||
%Cpu2 : 0.0 us, 0.0 sy, 0.0 ni, 94.7 id, 0.0 wa, 0.0 hi, 5.3 si, 0.0 st
|
||||
%Cpu3 : 5.3 us, 0.0 sy, 0.0 ni, 94.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
|
||||
%Cpu4 : 10.5 us, 15.8 sy, 0.0 ni, 73.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
|
||||
%Cpu5 : 0.0 us, 5.0 sy, 0.0 ni, 95.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
|
||||
%Cpu6 : 5.3 us, 0.0 sy, 0.0 ni, 94.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
|
||||
%Cpu7 : 5.3 us, 0.0 sy, 0.0 ni, 94.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
|
||||
```
|
||||
|
||||
要获取不包含百分比符号的 CPU 占用率:
|
||||
|
||||
```
|
||||
$ top -b -n1 | grep ^%Cpu | awk '{cpu+=$9}END{print "Current CPU Utilization is : " 100-cpu/NR}'
|
||||
Current CPU Utilization is : 21.05
|
||||
```
|
||||
|
||||
要获取包含百分比符号及保留两位小数的 CPU 占用率:
|
||||
|
||||
```
|
||||
$ top -b -n1 | grep ^%Cpu | awk '{cpu+=$9}END{printf("Current CPU Utilization is : %.2f%"), 100-cpu/NR}'
|
||||
Current CPU Utilization is : 14.81%
|
||||
```
|
||||
|
||||
### 方法-2:如何查看 Linux 下 CPU 的占用率?
|
||||
|
||||
我们可以使用如下命令的组合来达到此目的。在这种方法中,我们使用的是 `top`、`print`/`printf` 和 `awk` 命令的组合来获取 CPU 的占用率。
|
||||
|
||||
如果在单个输出中一起展示了所有的 CPU 的情况,那么你需要使用下面的方法。
|
||||
|
||||
```
|
||||
$ top -b -n1 | grep ^%Cpu
|
||||
%Cpu(s): 15.3 us, 7.2 sy, 0.8 ni, 69.0 id, 6.7 wa, 0.0 hi, 1.0 si, 0.0 st
|
||||
```
|
||||
|
||||
要获取不包含百分比符号的 CPU 占用率:
|
||||
|
||||
```
|
||||
$ top -b -n1 | grep ^%Cpu | awk '{print "Current CPU Utilization is : " 100-$8}'
|
||||
Current CPU Utilization is : 5.6
|
||||
```
|
||||
|
||||
要获取包含百分比符号及保留两位小数的 CPU 占用率:
|
||||
|
||||
```
|
||||
$ top -b -n1 | grep ^%Cpu | awk '{printf("Current CPU Utilization is : %.2f%"), 100-$8}'
|
||||
Current CPU Utilization is : 5.40%
|
||||
```
|
||||
|
||||
如下是一些细节:
|
||||
|
||||
* `top`:是一种用于查看当前 Linux 系统下正在运行的进程的非常好的命令。
|
||||
* `-b`:选项允许 `top` 命令切换至批处理的模式。当你从本地系统运行 `top` 命令至远程系统时,它将会非常有用。
|
||||
* `-n1`:迭代次数。
|
||||
* `^%Cpu`:过滤以 `%CPU` 开头的行。
|
||||
* `awk`:是一种专门用来做文本数据处理的强大命令。
|
||||
* `cpu+=$9`:对于每一行,将第 9 列添加至变量 `cpu`。
|
||||
* `printf`:该命令用于格式化和打印数据。
|
||||
* `%.2f%`:默认情况下,它打印小数点后保留 6 位的浮点数。使用后跟的格式来限制小数位数。
|
||||
* `100-cpu/NR`:最终打印出 CPU 平均占用率,即用 100 减去其并除以行数。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.2daygeek.com/linux-check-cpu-memory-swap-utilization-percentage/
|
||||
|
||||
作者:[Vinoth Kumar][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[An-DJ](https://github.com/An-DJ)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.2daygeek.com/author/vinoth/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.2daygeek.com/free-command-to-check-memory-usage-statistics-in-linux/
|
||||
[2]: https://www.2daygeek.com/smem-linux-memory-usage-statistics-reporting-tool/
|
||||
[3]: https://www.2daygeek.com/ps_mem-report-core-memory-usage-accurately-in-linux/
|
||||
[4]: https://www.2daygeek.com/linux-vmstat-command-examples-tool-report-virtual-memory-statistics/
|
||||
[5]: https://www.2daygeek.com/easy-ways-to-check-size-of-physical-memory-ram-in-linux/
|
||||
[6]: https://www.2daygeek.com/how-to-create-extend-swap-partition-in-linux-using-lvm/
|
||||
[7]: https://www.2daygeek.com/add-extend-increase-swap-space-memory-file-partition-linux/
|
||||
[8]: https://www.2daygeek.com/shell-script-create-add-extend-swap-space-linux/
|
||||
[9]: https://www.2daygeek.com/linux-top-command-linux-system-performance-monitoring-tool/
|
||||
[10]: https://www.2daygeek.com/linux-htop-command-linux-system-performance-resource-monitoring-tool/
|
||||
[11]: https://www.2daygeek.com/atop-system-process-performance-monitoring-tool/
|
||||
[12]: https://www.2daygeek.com/install-glances-advanced-real-time-linux-system-performance-monitoring-tool-on-centos-fedora-ubuntu-debian-opensuse-arch-linux/
|
||||
@@ -1,5 +1,5 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: translator: (mokshal)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: subject: (5 reasons to give Linux for the holidays)
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
jdh8383 is translating.
|
||||
|
||||
Tips for success when getting started with Ansible
|
||||
======
|
||||
|
||||

|
||||
|
||||
Ansible is an open source automation tool used to configure servers, install software, and perform a wide variety of IT tasks from one central location. It is a one-to-many agentless mechanism where all instructions are run from a control machine that communicates with remote clients over SSH, although other protocols are also supported.
|
||||
|
||||
While targeted for system administrators with privileged access who routinely perform tasks such as installing and configuring applications, Ansible can also be used by non-privileged users. For example, a database administrator using the `mysql` login ID could use Ansible to create databases, add users, and define access-level controls.
|
||||
|
||||
Let's go over a very simple example where a system administrator provisions 100 servers each day and must run a series of Bash commands on each one before handing it off to users.
|
||||
|
||||

|
||||
|
||||
This is a simple example, but should illustrate how easily commands can be specified in yaml files and executed on remote servers. In a heterogeneous environment, conditional statements can be added so that certain commands are only executed in certain servers (e.g., "only execute `yum` commands in systems that are not Ubuntu or Debian").
|
||||
|
||||
One important feature in Ansible is that a playbook describes a desired state in a computer system, so a playbook can be run multiple times against a server without impacting its state. If a certain task has already been implemented (e.g., "user `sysman` already exists"), then Ansible simply ignores it and moves on.
|
||||
|
||||
### Definitions
|
||||
|
||||
* **Tasks:**``A task is the smallest unit of work. It can be an action like "Install a database," "Install a web server," "Create a firewall rule," or "Copy this configuration file to that server."
|
||||
* **Plays:**``A play is made up of tasks. For example, the play: "Prepare a database to be used by a web server" is made up of tasks: 1) Install the database package; 2) Set a password for the database administrator; 3) Create a database; and 4) Set access to the database.
|
||||
* **Playbook:**``A playbook is made up of plays. A playbook could be: "Prepare my website with a database backend," and the plays would be 1) Set up the database server; and 2) Set up the web server.
|
||||
* **Roles:**``Roles are used to save and organize playbooks and allow sharing and reuse of playbooks. Following the previous examples, if you need to fully configure a web server, you can use a role that others have written and shared to do just that. Since roles are highly configurable (if written correctly), they can be easily reused to suit any given deployment requirements.
|
||||
* **Ansible Galaxy:**``Ansible [Galaxy][1] is an online repository where roles are uploaded so they can be shared with others. It is integrated with GitHub, so roles can be organized into Git repositories and then shared via Ansible Galaxy.
|
||||
|
||||
|
||||
|
||||
These definitions and their relationships are depicted here:
|
||||
|
||||

|
||||
|
||||
Please note this is just one way to organize the tasks that need to be executed. We could have split up the installation of the database and the web server into separate playbooks and into different roles. Most roles in Ansible Galaxy install and configure individual applications. You can see examples for installing [mysql][2] and installing [httpd][3].
|
||||
|
||||
### Tips for writing playbooks
|
||||
|
||||
The best source for learning Ansible is the official [documentation][4] site. And, as usual, online search is your friend. I recommend starting with simple tasks, like installing applications or creating users. Once you are ready, follow these guidelines:
|
||||
|
||||
* When testing, use a small subset of servers so that your plays execute faster. If they are successful in one server, they will be successful in others.
|
||||
* Always do a dry run to make sure all commands are working (run with `--check-mode` flag).
|
||||
* Test as often as you need to without fear of breaking things. Tasks describe a desired state, so if a desired state is already achieved, it will simply be ignored.
|
||||
* Be sure all host names defined in `/etc/ansible/hosts` are resolvable.
|
||||
* Because communication to remote hosts is done using SSH, keys have to be accepted by the control machine, so either 1) exchange keys with remote hosts prior to starting; or 2) be ready to type in "Yes" to accept SSH key exchange requests for each remote host you want to manage.
|
||||
* Although you can combine tasks for different Linux distributions in one playbook, it's cleaner to write a separate playbook for each distro.
|
||||
|
||||
|
||||
|
||||
### In the final analysis
|
||||
|
||||
Ansible is a great choice for implementing automation in your data center:
|
||||
|
||||
* It's agentless, so it is simpler to install than other automation tools.
|
||||
* Instructions are in YAML (though JSON is also supported) so it's easier than writing shell scripts.
|
||||
* It's open source software, so contribute back to it and make it even better!
|
||||
|
||||
|
||||
|
||||
How have you used Ansible to automate your data center? Share your experience in the comments.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/18/2/tips-success-when-getting-started-ansible
|
||||
|
||||
作者:[Jose Delarosa][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://opensource.com/users/jdelaros1
|
||||
[1]:https://galaxy.ansible.com/
|
||||
[2]:https://galaxy.ansible.com/bennojoy/mysql/
|
||||
[3]:https://galaxy.ansible.com/xcezx/httpd/
|
||||
[4]:http://docs.ansible.com/
|
||||
@@ -1,92 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Akira: The Linux Design Tool We’ve Always Wanted?)
|
||||
[#]: via: (https://itsfoss.com/akira-design-tool)
|
||||
[#]: author: (Ankush Das https://itsfoss.com/author/ankush/)
|
||||
|
||||
Akira: The Linux Design Tool We’ve Always Wanted?
|
||||
======
|
||||
|
||||
Let’s make it clear, I am not a professional designer – but I’ve used certain tools on Windows (like Photoshop, Illustrator, etc.) and [Figma][1] (which is a browser-based interface design tool). I’m sure there are a lot more design tools available for Mac and Windows.
|
||||
|
||||
Even on Linux, there is a limited number of dedicated [graphic design tools][2]. A few of these tools like [GIMP][3] and [Inkscape][4] are used by professionals as well. But most of them are not considered professional grade, unfortunately.
|
||||
|
||||
Even if there are a couple more solutions – I’ve never come across a native Linux application that could replace [Sketch][5], Figma, or Adobe **** XD. Any professional designer would agree to that, isn’t it?
|
||||
|
||||
### Is Akira going to replace Sketch, Figma, and Adobe XD on Linux?
|
||||
|
||||
Well, in order to develop something that would replace those awesome proprietary tools – [Alessandro Castellani][6] – came up with a [Kickstarter campaign][7] by teaming up with a couple of experienced developers –
|
||||
[Alberto Fanjul][8], [Bilal Elmoussaoui][9], and [Felipe Escoto][10].
|
||||
|
||||
So, yes, Akira is still pretty much just an idea- with a working prototype of its interface (as I observed in their [live stream session][11] via Kickstarter recently).
|
||||
|
||||
### If it does not exist, why the Kickstarter campaign?
|
||||
|
||||
![][12]
|
||||
|
||||
The aim of the Kickstarter campaign is to gather funds in order to hire the developers and take a few months off to dedicate their time in order to make Akira possible.
|
||||
|
||||
Nonetheless, if you want to support the project, you should know some details, right?
|
||||
|
||||
Fret not, we asked a couple of questions in their livestream session – let’s get into it…
|
||||
|
||||
### Akira: A few more details
|
||||
|
||||
![Akira prototype interface][13]
|
||||
Image Credits: Kickstarter
|
||||
|
||||
As the Kickstarter campaign describes:
|
||||
|
||||
> The main purpose of Akira is to offer a fast and intuitive tool to **create Web and Mobile interfaces** , more like **Sketch** , **Figma** , or **Adobe XD** , with a completely native experience for Linux.
|
||||
|
||||
They’ve also written a detailed description as to how the tool will be different from Inkscape, Glade, or QML Editor. Of course, if you want all the technical details, [Kickstarter][7] is the way to go. But, before that, let’s take a look at what they had to say when I asked some questions about Akira.
|
||||
|
||||
Q: If you consider your project – similar to what Figma offers – why should one consider installing Akira instead of using the web-based tool? Is it just going to be a clone of those tools – offering a native Linux experience or is there something really interesting to encourage users to switch (except being an open source solution)?
|
||||
|
||||
**Akira:** A native experience on Linux is always better and fast in comparison to a web-based electron app. Also, the hardware configuration matters if you choose to utilize Figma – but Akira will be light on system resource and you will still be able to do similar stuff without needing to go online.
|
||||
|
||||
Q: Let’s assume that it becomes the open source solution that Linux users have been waiting for (with similar features offered by proprietary tools). What are your plans to sustain it? Do you plan to introduce any pricing plans – or rely on donations?
|
||||
|
||||
**Akira** : The project will mostly rely on Donations (something like [Krita Foundation][14] could be an idea). But, there will be no “pro” pricing plans – it will be available for free and it will be an open source project.
|
||||
|
||||
So, with the response I got, it definitely seems to be something promising that we should probably support.
|
||||
|
||||
### Wrapping Up
|
||||
|
||||
What do you think about Akira? Is it just going to remain a concept? Or do you hope to see it in action?
|
||||
|
||||
Let us know your thoughts in the comments below.
|
||||
|
||||
![][15]
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/akira-design-tool
|
||||
|
||||
作者:[Ankush Das][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/ankush/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.figma.com/
|
||||
[2]: https://itsfoss.com/best-linux-graphic-design-software/
|
||||
[3]: https://itsfoss.com/gimp-2-10-release/
|
||||
[4]: https://inkscape.org/
|
||||
[5]: https://www.sketchapp.com/
|
||||
[6]: https://github.com/Alecaddd
|
||||
[7]: https://www.kickstarter.com/projects/alecaddd/akira-the-linux-design-tool/description
|
||||
[8]: https://github.com/albfan
|
||||
[9]: https://github.com/bilelmoussaoui
|
||||
[10]: https://github.com/Philip-Scott
|
||||
[11]: https://live.kickstarter.com/alessandro-castellani/live-stream/the-current-state-of-akira
|
||||
[12]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2019/01/akira-design-tool-kickstarter.jpg?resize=800%2C451&ssl=1
|
||||
[13]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2019/01/akira-mockup.png?ssl=1
|
||||
[14]: https://krita.org/en/about/krita-foundation/
|
||||
[15]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2019/01/akira-design-tool-kickstarter.jpg?fit=812%2C458&ssl=1
|
||||
@@ -1,114 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (LazyWolfLin)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (7 steps for hunting down Python code bugs)
|
||||
[#]: via: (https://opensource.com/article/19/2/steps-hunting-code-python-bugs)
|
||||
[#]: author: (Maria Mckinley https://opensource.com/users/parody)
|
||||
|
||||
7 steps for hunting down Python code bugs
|
||||
======
|
||||
Learn some tricks to minimize the time you spend tracking down the reasons your code fails.
|
||||

|
||||
|
||||
It is 3 pm on a Friday afternoon. Why? Because it is always 3 pm on a Friday when things go down. You get a notification that a customer has found a bug in your software. After you get over your initial disbelief, you contact DevOps to find out what is happening with the logs for your app, because you remember receiving a notification that they were being moved.
|
||||
|
||||
Turns out they are somewhere you can't get to, but they are in the process of being moved to a web application—so you will have this nifty application for searching and reading them, but of course, it is not finished yet. It should be up in a couple of days. I know, totally unrealistic situation, right? Unfortunately not; it seems logs or log messages often come up missing at just the wrong time. Before we track down the bug, a public service announcement: Check your logs to make sure they are where you think they are and logging what you think they should log, regularly. Amazing how these things just change when you aren't looking.
|
||||
|
||||
OK, so you found the logs or tried the call, and indeed, the customer has found a bug. Maybe you even think you know where the bug is.
|
||||
|
||||
You immediately open the file you think might be the problem and start poking around.
|
||||
|
||||
### 1. Don't touch your code yet
|
||||
|
||||
Go ahead and look at it, maybe even come up with a hypothesis. But before you start mucking about in the code, take that call that creates the bug and turn it into a test. This will be an integration test because although you may have suspicions, you do not yet know exactly where the problem is.
|
||||
|
||||
Make sure this test fails. This is important because sometimes the test you make doesn't mimic the broken call; this is especially true if you are using a web or other framework that can obfuscate the tests. Many things may be stored in variables, and it is unfortunately not always obvious, just by looking at the test, what call you are making in the test. I'm not going to say that I have created a test that passed when I was trying to imitate a broken call, but, well, I have, and I don't think that is particularly unusual. Learn from my mistakes.
|
||||
|
||||
### 2. Write a failing test
|
||||
|
||||
Now that you have a failing test or maybe a test with an error, it is time to troubleshoot. But before you do that, let's do a review of the stack, as this makes troubleshooting easier.
|
||||
|
||||
The stack consists of all of the tasks you have started but not finished. So, if you are baking a cake and adding the flour to the batter, then your stack would be:
|
||||
|
||||
* Make cake
|
||||
* Make batter
|
||||
* Add flour
|
||||
|
||||
|
||||
|
||||
You have started making your cake, you have started making the batter, and you are adding the flour. Greasing the pan is not on the list since you already finished that, and making the frosting is not on the list because you have not started that.
|
||||
|
||||
If you are fuzzy on the stack, I highly recommend playing around on [Python Tutor][1], where you can watch the stack as you execute lines of code.
|
||||
|
||||
Now, if something goes wrong with your Python program, the interpreter helpfully prints out the stack for you. This means that whatever the program was doing at the moment it became apparent that something went wrong is on the bottom.
|
||||
|
||||
### 3. Always check the bottom of the stack first
|
||||
|
||||
Not only is the bottom of the stack where you can see which error occurred, but often the last line of the stack is where you can find the issue. If the bottom doesn't help, and your code has not been linted in a while, it is amazing how helpful it can be to run. I recommend pylint or flake8. More often than not, it points right to where there is an error that I have been overlooking.
|
||||
|
||||
If the error is something that seems obscure, your next move might just be to Google it. You will have better luck if you don't include information that is relevant only to your code, like the name of variables, files, etc. If you are using Python 3 (which you should be), it's helpful to include the 3 in the search; otherwise, Python 2 solutions tend to dominate the top.
|
||||
|
||||
Once upon a time, developers had to troubleshoot without the benefit of a search engine. This was a dark time. Take advantage of all the tools available to you.
|
||||
|
||||
Unfortunately, sometimes the problem occurred earlier and only became apparent during the line executed on the bottom of the stack. Think about how forgetting to add the baking powder becomes obvious when the cake doesn't rise.
|
||||
|
||||
It is time to look up the stack. Chances are quite good that the problem is in your code, and not Python core or even third-party packages, so scan the stack looking for lines in your code first. Plus it is usually much easier to put a breakpoint in your own code. Stick the breakpoint in your code a little further up the stack and look around to see if things look like they should.
|
||||
|
||||
"But Maria," I hear you say, "this is all helpful if I have a stack trace, but I just have a failing test. Where do I start?"
|
||||
|
||||
Pdb, the Python Debugger.
|
||||
|
||||
Find a place in your code where you know this call should hit. You should be able to find at least one place. Stick a pdb break in there.
|
||||
|
||||
#### A digression
|
||||
|
||||
Why not a print statement? I used to depend on print statements. They still come in handy sometimes. But once I started working with complicated code bases, and especially ones making network calls, print just became too slow. I ended up with print statements all over the place, I lost track of where they were and why, and it just got complicated. But there is a more important reason to mostly use pdb. Let's say you put a print statement in and discover that something is wrong—and must have gone wrong earlier. But looking at the function where you put the print statement, you have no idea how you got there. Looking at code is a great way to see where you are going, but it is terrible for learning where you've been. And yes, I have done a grep of my code base looking for where a function is called, but this can get tedious and doesn't narrow it down much with a popular function. Pdb can be very helpful.
|
||||
|
||||
You follow my advice, and put in a pdb break and run your test. And it whooshes on by and fails again, with no break at all. Leave your breakpoint in, and run a test already in your test suite that does something very similar to the broken test. If you have a decent test suite, you should be able to find a test that is hitting the same code you think your failed test should hit. Run that test, and when it gets to your breakpoint, do a `w` and look at the stack. If you have no idea by looking at the stack how/where the other call may have gone haywire, then go about halfway up the stack, find some code that belongs to you, and put a breakpoint in that file, one line above the one in the stack trace. Try again with the new test. Keep going back and forth, moving up the stack to figure out where your call went off the rails. If you get all the way up to the top of the trace without hitting a breakpoint, then congratulations, you have found the issue: Your app was spelled wrong. No experience here, nope, none at all.
|
||||
|
||||
### 4. Change things
|
||||
|
||||
If you still feel lost, try making a new test where you vary something slightly. Can you get the new test to work? What is different? What is the same? Try changing something else. Once you have your test, and maybe additional tests in place, it is safe to start changing things in the code to see if you can narrow down the problem. Remember to start troubleshooting with a fresh commit so you can easily back out changes that do not help. (This is a reference to version control, if you aren't using version control, it will change your life. Well, maybe it will just make coding easier. See "[A Visual Guide to Version Control][2]" for a nice introduction.)
|
||||
|
||||
### 5. Take a break
|
||||
|
||||
In all seriousness, when it stops feeling like a fun challenge or game and starts becoming really frustrating, your best course of action is to walk away from the problem. Take a break. I highly recommend going for a walk and trying to think about something else.
|
||||
|
||||
### 6. Write everything down
|
||||
|
||||
When you come back, if you aren't suddenly inspired to try something, write down any information you have about the problem. This should include:
|
||||
|
||||
* Exactly the call that is causing the problem
|
||||
* Exactly what happened, including any error messages or related log messages
|
||||
* Exactly what you were expecting to happen
|
||||
* What you have done so far to find the problem and any clues that you have discovered while troubleshooting
|
||||
|
||||
|
||||
|
||||
Sometimes this is a lot of information, but trust me, it is really annoying trying to pry information out of someone piecemeal. Try to be concise, but complete.
|
||||
|
||||
### 7. Ask for help
|
||||
|
||||
I often find that just writing down all the information triggers a thought about something I have not tried yet. Sometimes, of course, I realize what the problem is immediately after hitting the submit button. At any rate, if you still have not thought of anything after writing everything down, try sending an email to someone. First, try colleagues or other people involved in your project, then move on to project email lists. Don't be afraid to ask for help. Most people are kind and helpful, and I have found that to be especially true in the Python community.
|
||||
|
||||
Maria McKinley will present [Hunting the Bugs][3] at [PyCascades 2019][4], February 23-24 in Seattle.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/2/steps-hunting-code-python-bugs
|
||||
|
||||
作者:[Maria Mckinley][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/parody
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: http://www.pythontutor.com/
|
||||
[2]: https://betterexplained.com/articles/a-visual-guide-to-version-control/
|
||||
[3]: https://2019.pycascades.com/talks/hunting-the-bugs
|
||||
[4]: https://2019.pycascades.com/
|
||||
@@ -1,5 +1,5 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: translator: (An-DJ)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
Ansible 初学者成功指南
|
||||
======
|
||||
|
||||

|
||||
|
||||
Ansible 是一个开源自动化工具,可以从中央控制节点统一配置服务器、安装软件或执行各种 IT 任务。它采用一对多、<ruby>无客户端<rt>agentless</rt></ruby>的机制,从控制节点上通过 SSH 发送指令给远端的客户机来完成任务(当然除了 SSH 外也可以用别的协议)。
|
||||
|
||||
Ansible 的主要使用群体是系统管理员,他们经常会周期性地执行一些安装、配置应用的工作。尽管如此,一些非特权用户也可以使用 Ansible,例如数据库管理员就可以通过 Ansible 用 `mysql` 这个用户来创建数据库、添加数据库用户、定义访问权限等。
|
||||
|
||||
让我们来看一个简单的使用场景,一位系统管理员每天要配置 100 台服务器,并且必须在每台机器上执行一系列 Bash 命令,然后交付给用户。
|
||||
|
||||

|
||||
|
||||
这是个简单的例子,但应该能够证明:在 yaml 文件里写好命令然后在远程服务器上运行,是一件非常轻松的事。而且如果运行环境不同,就可以加入判断条件,指明某些命令只能在特定的服务器上运行(如:只在那些不是 Ubuntu 或 Debian 的系统上运行 `yum` 命令)。
|
||||
|
||||
Ansible 的一个重要特性是用 playbook 来描述一个计算机系统的最终状态,所以一个 playbook 可以在服务器上反复执行而不影响其最终状态(译者注:即是幂等的)。如果某个任务已经被实施过了(如,“用户 `sysman` 已经存在”),那么 Ansible 就会忽略它继续执行后续的任务。
|
||||
|
||||
### 定义
|
||||
|
||||
* **<ruby>任务<rt>Tasks</rt></ruby>:** task 是工作的最小单位,它可以是个动作,比如“安装一个数据库服务”、“安装一个 web 服务器”、“创建一条防火墙规则”或者“把这个配置文件拷贝到那个服务器上去”。
|
||||
* **<ruby>战术动作<rt>Plays</rt></ruby>:** play 由 task 组成,例如,一个 play 的内容是要:“设置一个数据库,给 web 服务用”,这就包含了如下任务:1)安装数据库包;2)设置数据库管理员密码;3)创建数据库实例;4)为该实例分配权限。
|
||||
* **<ruby>战术手册<rt>Playbook</rt></ruby>:**(译者注:playbook 原指美式橄榄球队的[战术手册][5]) playbook 由 play 组成,一个 playbook 可能像这样:“设置我的网站,包含后端数据库”,其中的 play 包括:1)设置数据库服务器;2)设置 web 服务器。
|
||||
* **<ruby>角色<rt>Roles</rt></ruby>:** Role 用来保存和组织 playbook,以便分享和再次使用它们。还拿上个例子来说,如果你需要一个全新的 web 服务器,就可以用别人已经写好并分享出来的 role 来设置。因为 role 是高度可配置的(如果编写正确的话),可以根据部署需求轻松地复用它们。
|
||||
* **<ruby>Ansible 星系<rt>Ansible Galaxy</rt></ruby>:** [Ansible Galaxy][1] 是一个在线仓库,里面保存的是由社区成员上传的 role,方便彼此分享。它与 GitHub 紧密集成,因此这些 role 可以先在 Git 仓库里组织好,然后通过 Ansible Galaxy 分享出来。
|
||||
|
||||
|
||||
这些定义以及它们之间的关系可以用下图来描述:
|
||||
|
||||

|
||||
|
||||
请注意上面的例子只是组织任务的方式之一,我们当然也可以把安装数据库和安装 web 服务器的 playbook 拆开,放到不同的 role 里。Ansible Galaxy 上最常见的 role 是独立安装配置每个应用服务,你可以参考这些安装 [mysql][2] 和 [httpd][3] 的例子。
|
||||
|
||||
### 编写 playbook 的小贴士
|
||||
|
||||
学习 Ansible 最好的资源是其[官方文档][4]。另外,像学习其他东西一样,搜索引擎是你的好朋友。我推荐你从一些简单的任务开始,比如安装应用或创建用户。下面是一些有用的指南:
|
||||
|
||||
* 在测试的时候少选几台服务器,这样你的 play 可以执行的更快一些。如果它们在一台机器上执行成功,在其他机器上也没问题。
|
||||
* 总是在真正运行前做一次<ruby>测试<rt>dry run</rt></ruby>以确保所有的命令都能正确执行(要运行测试,加上 `--check-mode` 参数 )。
|
||||
* 尽可能多做测试,别担心搞砸。任务里描述的是所需的状态,如果系统已经达到预期状态,任务会被简单地忽略掉。
|
||||
* 确保在 `/etc/ansible/hosts` 里定义的主机名都可以被正确解析。
|
||||
* 因为是用 SSH 与远程主机通信,主控节点必须要接受密钥,所以你面临如下选择:1)要么在正式使用之前就做好与远程主机的密钥交换工作;2)要么在开始管理某台新的远程主机时做好准备输入“Yes”,因为你要接受对方的 SSH 密钥交换请求(译者注:还有另一个不那么安全的选择,修改主控节点的 ssh 配置文件,将 `StrictHostKeyChecking` 设置成“no”)。
|
||||
* 尽管你可以在同一个 playbook 内把不同 Linux 发行版的任务整合到一起,但为每个发行版单独编写 playbook 会更明晰一些。
|
||||
|
||||
|
||||
### 总结一下
|
||||
|
||||
Ansible 是你在数据中心里实施运维自动化的好选择,因为它:
|
||||
|
||||
* 无需客户端,所以比其他自动化工具更易安装。
|
||||
* 将指令保存在 YAML 文件中(虽然也支持 JSON),比写 shell 脚本更简单。
|
||||
* 开源,因此你也可以做出自己的贡献,让它更加强大!
|
||||
|
||||
|
||||
你是怎样使用 Ansible 让数据中心更加自动化的呢?请在评论中分享您的经验。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/18/2/tips-success-when-getting-started-ansible
|
||||
|
||||
作者:[Jose Delarosa][a]
|
||||
译者:[jdh8383](https://github.com/jdh8383)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://opensource.com/users/jdelaros1
|
||||
[1]:https://galaxy.ansible.com/
|
||||
[2]:https://galaxy.ansible.com/bennojoy/mysql/
|
||||
[3]:https://galaxy.ansible.com/xcezx/httpd/
|
||||
[4]:http://docs.ansible.com/
|
||||
[5]:https://usafootball.com/football-playbook/
|
||||
@@ -0,0 +1,91 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Akira: The Linux Design Tool We’ve Always Wanted?)
|
||||
[#]: via: (https://itsfoss.com/akira-design-tool)
|
||||
[#]: author: (Ankush Das https://itsfoss.com/author/ankush/)
|
||||
|
||||
Akira:我们一直想要的 Linux 设计工具?
|
||||
======
|
||||
|
||||
先说一下,我不是一个专业的设计师 - 但我在 Windows 上使用了某些工具(如 Photoshop、Illustrator 等)和 [Figma] [1](这是一个基于浏览器的界面设计工具)。我相信 Mac 和 Windows 上还有更多的设计工具。
|
||||
|
||||
即使在 Linux 上,也只有有限的专用[图形设计工具][2]。其中一些工具如 [GIMP][3] 和 [Inkscape][4] 也被专业人士使用。但不幸的是,它们中的大多数都不被视为专业级。
|
||||
|
||||
即使有更多解决方案 - 我也从未遇到过可以取代 [Sketch][5]、Figma 或 Adobe XD 的原生 Linux 应用。任何专业设计师都同意这点,不是吗?
|
||||
|
||||
### Akira 是否会在 Linux 上取代 Sketch、Figma 和 Adobe XD?
|
||||
|
||||
所以,为了开发一些能够取代那些专有工具的应用 - [Alessandro Castellani][6] 发起了一个 [Kickstarter 活动][7],并与几位经验丰富的开发人员 [Alberto Fanjul][8]、[Bilal Elmoussaoui][9] 和 [Felipe Escoto][10] 组队合作。
|
||||
|
||||
是的,Akira 仍然只是一个想法,只有一个界面原型(正如我最近在 Kickstarter 的[直播流][11]中看到的那样)。
|
||||
|
||||
### 如果它还没有,为什么会发起 Kickstarter 活动?
|
||||
|
||||
![][12]
|
||||
|
||||
Kickstarter 活动的目的是收集资金,以便雇用开发人员,并花几个月的时间开发,以使 Akira 成为可能。
|
||||
|
||||
尽管如此,如果你想支持这个项目,你应该知道一些细节,对吧?
|
||||
|
||||
不用担心,我们在他们的直播中问了几个问题 - 让我们看下
|
||||
|
||||
### Akira:更多细节
|
||||
|
||||
![Akira prototype interface][13]
|
||||
图片来源:Kickstarter
|
||||
|
||||
如 Kickstarter 活动描述的那样:
|
||||
|
||||
> Akira 的主要目的是提供一个快速而直观的工具来**创建 Web 和移动界面**,更像是 **Sketch**、**Figma** 或 **Adobe XD**,并且是 Linux 原生体验。
|
||||
|
||||
他们还详细描述了该工具与 Inkscape、Glade 或 QML Editor 的不同之处。当然,如果你想要所有的技术细节,请查看 [Kickstarter][7]。但是,在此之前,让我们看一看当我询问有关 Akira 的一些问题时他们说了些什么。
|
||||
|
||||
问:如果你认为你的项目类似于 Figma - 人们为什么要考虑安装 Akira 而不是使用基于网络的工具?它是否只是这些工具的克隆 - 提供原生 Linux 体验,还是有一些非常有趣的东西可以鼓励用户切换(除了是开源解决方案之外)?
|
||||
|
||||
** Akira:** 与基于网络的 electron 应用相比,Linux 原生体验总是更好、更快。此外,如果你选择使用 Figma,硬件配置也很重要 - 但 Akira 将会占用很少的系统资源,并且你可以在不需要上网的情况下完成类似工作。
|
||||
|
||||
问:假设它成为了 Linux用户一直在等待的开源方案(拥有专有工具的类似功能)。你有什么维护计划?你是否计划引入定价 - 或依赖捐赠?
|
||||
|
||||
**Akira:**该项目主要依靠捐赠(类似于 [Krita 基金会][14] 这样的想法)。但是,不会有“专业版”计划 - 它将免费提供,它将是一个开源项目。
|
||||
|
||||
根据我得到的回答,它看起来似乎很有希望,我们应该支持。
|
||||
|
||||
### 总结
|
||||
|
||||
你怎么认为 Akira?它只是一个概念吗?或者你希望看到进展?
|
||||
|
||||
请在下面的评论中告诉我们你的想法。
|
||||
|
||||
![][15]
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/akira-design-tool
|
||||
|
||||
作者:[Ankush Das][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/ankush/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.figma.com/
|
||||
[2]: https://itsfoss.com/best-linux-graphic-design-software/
|
||||
[3]: https://itsfoss.com/gimp-2-10-release/
|
||||
[4]: https://inkscape.org/
|
||||
[5]: https://www.sketchapp.com/
|
||||
[6]: https://github.com/Alecaddd
|
||||
[7]: https://www.kickstarter.com/projects/alecaddd/akira-the-linux-design-tool/description
|
||||
[8]: https://github.com/albfan
|
||||
[9]: https://github.com/bilelmoussaoui
|
||||
[10]: https://github.com/Philip-Scott
|
||||
[11]: https://live.kickstarter.com/alessandro-castellani/live-stream/the-current-state-of-akira
|
||||
[12]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2019/01/akira-design-tool-kickstarter.jpg?resize=800%2C451&ssl=1
|
||||
[13]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2019/01/akira-mockup.png?ssl=1
|
||||
[14]: https://krita.org/en/about/krita-foundation/
|
||||
[15]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2019/01/akira-design-tool-kickstarter.jpg?fit=812%2C458&ssl=1
|
||||
@@ -0,0 +1,110 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (LazyWolfLin)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (7 steps for hunting down Python code bugs)
|
||||
[#]: via: (https://opensource.com/article/19/2/steps-hunting-code-python-bugs)
|
||||
[#]: author: (Maria Mckinley https://opensource.com/users/parody)
|
||||
|
||||
7 步检查 Python 代码错误
|
||||
======
|
||||
了解一些技巧助你减少代码查错时间。
|
||||

|
||||
|
||||
在周五的下午三点钟。为什么是这个时间?因为事情总会在周五下午三点钟发生。你收到一条通知,客户发现你的软件出现一个错误。在有了初步的怀疑后,你联系运维,查看你的软件日志以了解发生了什么,因为你记得收到过日志已经移动了的通知。
|
||||
|
||||
结果这些日志被转移到了你获取不到的地方,但他们正在导到一个网页应用中——所以你将可以用这个漂亮的应用来检索日志,但是,这个应用现在还没完成。这个应用预计会在几天内完成。我知道,这完全不符合实际情况,对吧?然而并不是,日志或者日志消息似乎经常在错误的时间出现缺失。在我们开始查错前,一个忠告:经常检查你的日志以确保他们在你认为它们应该在的地方并记录你认为它们应该记的东西。当你不检查的时候,这些东西往往会发生令人惊讶的变化。
|
||||
|
||||
好的,你找到了日志或者尝试了呼叫运维人员,而客户确实发现了一个错误。甚至你可能认为你已经知道错误在哪儿。
|
||||
|
||||
你立即打开你认为可能有问题的文件并开始查错。
|
||||
|
||||
### 1. 不要碰你的代码
|
||||
|
||||
阅读代码,你甚至可能会想到一个假设。但是在开始修改你的代码前,请重现导致错误的调用并把它变成一个测试。这将是一个集成测试,因为你可能还有其他疑问,目前你还没能准确地知道问题在哪儿。
|
||||
|
||||
确保这个测试是失败的。这很重要,因为有时你的测试不能重现失败的调用,尤其是你使用了可以混淆测试的 web 或者其他框架。很多东西可能被存储在变量中,但遗憾的是,只通过观察测试,你在测试里调用的东西并不总是明显可见的。当我尝试着重现这个失败的调用时,我不准备说我创建了一个新测试,但是,对的,我确实已经创建了新的一个测试,但我不认为这是特别不寻常的。从自己的错误中吸取教训。
|
||||
|
||||
### 2. 编写错误的测试
|
||||
|
||||
现在,你有了一个失败的测试或者可能是一个带有错误的测试,那么是时候解决问题了。但是在你开干之前,让我们先检查下调用栈,因为这样可以更轻松地解决问题。
|
||||
|
||||
调用栈包括你已经启动但尚未完成地所有任务。因此,比如你正在烤蛋糕并准备往面糊里加面粉,那你的调用栈将是:
|
||||
|
||||
* 做蛋糕
|
||||
* 打面糊
|
||||
* 加面粉
|
||||
|
||||
你已经开始做蛋糕,开始打面糊,而你现在正在加面粉。往锅底抹油不在这个列表中因为你已经完成了,而做糖霜不在这个列表上因为你还没开始做。
|
||||
|
||||
如果你对调用栈不清楚,我强烈建议你使用 [Python Tutor][1],它能帮你在执行代码时观察调用栈。
|
||||
|
||||
现在,如果你的 Python 程序出现了错误,接收器会帮你打印出当前调用栈。这意味着无论那一时刻程序在做什么,很明显错误发生在调用栈的底部。
|
||||
|
||||
### 3. 始终先检查 stack 的底部
|
||||
|
||||
你不仅能在栈底看到发生了哪个错误,而且通常可以在调用栈的最后一行发现问题。如果栈底对你没有帮助,而你的代码还没有经过代码分析,那么使用代码分析非常有用。我推荐 pylint 或者 flake8。通常情况下,它会指出我一直忽略的错误的地方。
|
||||
|
||||
如果对错误看起来很迷惑,你下一步行动可能是用 Google 搜索它。如果你搜索的内容不包含你的代码的相关信息,如变量名、文件等,那你将获得更好的搜索结果。如果你使用的是 Python 3(你应该使用它),那么搜索内容包含 Python 3 是有帮助的,否则 Python 2 的解决方案往往会占据大多数。
|
||||
|
||||
很久以前,开发者需要在没有搜索引擎的帮助下解决问题。那是一段黑暗的时光。充分利用你可以使用的所有工具。
|
||||
|
||||
不幸的是,有时候问题发生得比较早但只有在调用栈底部执行的地方才变得明显。就像当蛋糕没有膨胀时,忘记加发酵粉的事才被发现。
|
||||
|
||||
那就该检查整个调用栈。问题更可能在你的代码而不是 Python 标准库或者第三方包,所以先检查调用栈内你的代码。另外,在你的代码中放置断点通常会更容易检查代码。在调用栈的代码中放置断点然后看看周围是否如你预期。
|
||||
|
||||
“但是,玛丽,”我听到你说,“如果我有一个调用栈,那这些都是有帮助的,但我只有一个失败的测试。我该从哪里开始?”
|
||||
|
||||
Pdb, 一个 Python 调试器。
|
||||
|
||||
找到你代码里会被这个调用命中的地方。你应该能够找到至少一个这样的地方。在那里打上一个 pdb 的断点。
|
||||
|
||||
#### 一句题外话
|
||||
|
||||
为什么不使用 print 语句呢?我曾经依赖于 print 语句。有时候,他们仍然派得上用场。但当我开始处理复杂的代码库,尤其是有网络调用的代码库,print 语句就变得太慢了。我最终得到所有打印出来的数据,但我没法追踪他们的位置和原因,而且他们变得复杂了。但是主要使用 pdb 还有一个更重要的原因。假设你添加一条 print 语句去发现错误问题,而且 print 语句必须早于错误出现的地方。但是,看看你放 print 语句的函数,你不知道你的代码是怎么执行到那个位置的。查看代码是寻找调用路径的好方法,但看你以前写的代码是恐怖的。是的,我会用 grep 处理我的代码库以寻找调用函数的地方,但这会变得乏味,而且搜索一个通用函数时并不能缩小搜索范围。Pdb 就变得非常有用。
|
||||
|
||||
你遵循我的建议,打上 pdb 断点并运行你的测试。然而测试再次失败,但是没有任何一个断点被打到。保留你的断点,并运行测试套件中一个同这个失败的测试非常相似的测试。如果你有个不错的测试,你应该能够找到一个测试。它会击中了你认为你的失败测试应该击中的代码。运行这个测试,然后当它打到你的断点,按下 `w` 并检查调用栈。如果你不知道如何查看因为其他调用而变得混乱的调用栈,那么在调用栈的中间找到属于你的代码,并在其中堆栈中代码的上一行放置一个断点。再试一次新的测试。如果仍然没打到断点,那么继续,向上追踪调用栈并找出你的调用在哪里脱轨了。如果你一直没有打到断点,最后到了追踪的顶部,那么恭喜你,你发现了问题:你的应用程序拼写错了。没有经验,没有经验,一点都没有经验。
|
||||
|
||||
### 4. 修改代码
|
||||
|
||||
如果你仍觉得迷惑,在你稍微改变了一些的地方尝试新的测试。你能让新的测试跑起来么?有什么不同的呢?有什么相同的呢?尝试改变别的东西。当你有了你的测试,可能也还有其他测试,那就可以开始安全地修改代码,确定是否可以缩小问题范围。记得从一个新提交开始解决问题,以便于可以轻松地撤销无效地更改。(这就是版本控制,如果你没有使用版本控制,这将会改变你的生活。好吧,可能它只是让编码更容易。查阅“版本控制可视指南”,以了解更多。)
|
||||
|
||||
### 5. 休息一下
|
||||
|
||||
尽管如此,当它不再感觉起来像一个有趣的挑战或者游戏而开始变得令人沮丧时,你最好的举措是脱离这个问题。休息一下。我强烈建议你去散步并尝试考虑别的事情。
|
||||
|
||||
### 6. 把一切写下来
|
||||
|
||||
当你回来了,如果你没有突然受到启发,那就把你关于这个问题所知的每一个点信息写下来。这应该包括:
|
||||
|
||||
* 真正造成问题的调用
|
||||
* 真正发生了什么,包括任何错误信息或者相关的日志信息
|
||||
* 你真正期望发生什么
|
||||
* 到目前为止,为了找出问题,你做了什么工作;以及解决问题中你发现的任何线索。
|
||||
|
||||
有时这里有很多信息,但相信我,从零碎中挖掘信息是很烦人。所以尽量简洁,但是要完整。
|
||||
|
||||
### 7. 寻求帮助
|
||||
|
||||
我经常发现写下所有信息能够启迪我想到还没尝试过的东西。当然,有时候我在点击提交按钮后立刻意识到问题是是什么。无论如何,当你在写下所有东西仍一无所获,那就试试向他人发邮件求助。首先是你的同事或者其他参与你的项目的人,然后是项目的邮件列表。不要害怕向人求助。大多数人都是友善和乐于助人的,我发现在 Python 社区里尤其如此。
|
||||
|
||||
Maria McKinley 将在 [PyCascades 2019][4] 发表[代码查错][3],二月 23-24,于西雅图。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/2/steps-hunting-code-python-bugs
|
||||
|
||||
作者:[Maria Mckinley][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[LazyWolfLin](https://github.com/LazyWolfLin)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/parody
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: http://www.pythontutor.com/
|
||||
[2]: https://betterexplained.com/articles/a-visual-guide-to-version-control/
|
||||
[3]: https://2019.pycascades.com/talks/hunting-the-bugs
|
||||
[4]: https://2019.pycascades.com/
|
||||
@@ -1,225 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (An-DJ)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How To Check CPU, Memory And Swap Utilization Percentage In Linux?)
|
||||
[#]: via: (https://www.2daygeek.com/linux-check-cpu-memory-swap-utilization-percentage/)
|
||||
[#]: author: (Vinoth Kumar https://www.2daygeek.com/author/vinoth/)
|
||||
|
||||
如何查看Linux下CPU,内存和Swap(交换分区)的占用率?
|
||||
======
|
||||
|
||||
在Linux下有很多可用的命令和选项来查看内存占用情况,但是我并没有看见关于内存利用率的更多的信息。
|
||||
|
||||
在大多数情况下我们只单独查看内存使用情况,并没有考虑占用的百分比究竟是多少。
|
||||
|
||||
如果你想要了解这些信息,那你看这篇文章就对了。
|
||||
|
||||
我们将会详细地在这里帮助你解决这个问题。
|
||||
|
||||
这篇教程将会帮助你在面对Linux服务器下频繁内存高占用情况时,确定内存使用情况。
|
||||
|
||||
但是在同时,如果你使用的是`free -m`或者`free -g`,占用情况描述地并不是十分清楚。
|
||||
|
||||
这些格式化命令属于Linux高级命令。它将会对Linux专家和中等水平Linux使用者非常有用。
|
||||
|
||||
### 方法-1:如何查看Linux下内存占用率?
|
||||
|
||||
我们可以使用下面命令的组合来达到此目的。在该方法中,我们使用的是`free`和`awk`命令的组合来获取内存占用率。
|
||||
|
||||
如果你正在寻找其他有关于内存的文章,你可以导航到如下链接。这些文章有 **[free命令][1]** , **[smem命令][2]** , **[ps_mem命令][3]** , **[vmstat命令][4]** 及 **[多种方式来查看物理内存大小][5]**.
|
||||
|
||||
对于获取不包含百分比符号的`内存`占用率:
|
||||
|
||||
```
|
||||
$ free -t | awk 'NR == 2 {print "Current Memory Utilization is : " $3/$2*100}'
|
||||
或
|
||||
$ free -t | awk 'FNR == 2 {print "Current Memory Utilization is : " $3/$2*100}'
|
||||
|
||||
Current Memory Utilization is : 20.4194
|
||||
```
|
||||
|
||||
对于获取不包含百分比符号的`Swap(交换分区)`占用率:
|
||||
|
||||
```
|
||||
$ free -t | awk 'NR == 3 {print "Current Swap Utilization is : " $3/$2*100}'
|
||||
或
|
||||
$ free -t | awk 'FNR == 3 {print "Current Swap Utilization is : " $3/$2*100}'
|
||||
|
||||
Current Swap Utilization is : 0
|
||||
```
|
||||
|
||||
对于获取包含百分比符号及保留两位小数的`内存`占用率:
|
||||
|
||||
```
|
||||
$ free -t | awk 'NR == 2 {printf("Current Memory Utilization is : %.2f%"), $3/$2*100}'
|
||||
或
|
||||
$ free -t | awk 'FNR == 2 {printf("Current Memory Utilization is : %.2f%"), $3/$2*100}'
|
||||
|
||||
Current Memory Utilization is : 20.42%
|
||||
```
|
||||
|
||||
对于获取包含百分比符号及保留两位小数的`Swap(交换分区)`占用率:
|
||||
|
||||
```
|
||||
$ free -t | awk 'NR == 3 {printf("Current Swap Utilization is : %.2f%"), $3/$2*100}'
|
||||
或
|
||||
$ free -t | awk 'FNR == 3 {printf("Current Swap Utilization is : %.2f%"), $3/$2*100}'
|
||||
|
||||
Current Swap Utilization is : 0.00%
|
||||
```
|
||||
|
||||
如果你正在寻找有关于内存的其他文章,你可以导航至如下链接。这些链接有 **[使用LVM(逻辑盘卷管理,Logical Volume Manager)创建和扩展Swap交换分区][6]** , **[多种方式创建或扩展Swap交换分区][7]** 和 **[多种方式创建/删除和挂载交换分区文件][8]**。
|
||||
|
||||
键入free命令会更好地作出阐释:
|
||||
|
||||
```
|
||||
$ free
|
||||
total used free shared buff/cache available
|
||||
Mem: 15867 3730 9868 1189 2269 10640
|
||||
Swap: 17454 0 17454
|
||||
Total: 33322 3730 27322
|
||||
```
|
||||
|
||||
如下是一些细节:
|
||||
|
||||
* **`free:`** free是一个标准命令,用于在Linux下查看内存使用情况。
|
||||
* **`awk:`** awk是一个专门用来做文本数据处理的强大命令。
|
||||
* **`FNR == 2:`** 该命令给出了对于每一个输入文件的行数。其基本上用于挑选出给定的行(针对于这里,它选择的是行数为2的行)
|
||||
* **`NR == 2:`** 该命令给出了处理的行总数。其基本上用于过滤给出的行(针对于这里,它选择的是行数为2的行)
|
||||
* **`$3/$2*100:`** 该命令将列3除以列2并将结果乘以100。
|
||||
* **`printf:`** 该命令用于格式化和打印数据。
|
||||
* **`%.2f%:`** 默认情况下,其打印小数点后保留6位的浮点数。使用后跟的格式来约束小数位。
|
||||
|
||||
|
||||
|
||||
### 方法-2:如何查看Linux下内存占用率?
|
||||
|
||||
我们可以使用下面命令的组合来达到此目的。在这种方法中,我们使用`free`,`grep`和`awk`命令的组合来获取内存占用率。
|
||||
|
||||
对于获取不包含百分比符号的`内存`占用率:
|
||||
|
||||
```
|
||||
$ free -t | grep Mem | awk '{print "Current Memory Utilization is : " $3/$2*100}'
|
||||
Current Memory Utilization is : 20.4228
|
||||
```
|
||||
|
||||
对于获取不包含百分比符号的`Swap(交换分区)`占用率:
|
||||
|
||||
```
|
||||
$ free -t | grep Swap | awk '{print "Current Swap Utilization is : " $3/$2*100}'
|
||||
Current Swap Utilization is : 0
|
||||
```
|
||||
|
||||
对于获取包含百分比符号及保留两位小数的`内存`占用率:
|
||||
|
||||
```
|
||||
$ free -t | grep Mem | awk '{printf("Current Memory Utilization is : %.2f%"), $3/$2*100}'
|
||||
Current Memory Utilization is : 20.43%
|
||||
```
|
||||
|
||||
对于获取包含百分比符号及保留两位小数的`Swap(交换空间)`占用率:
|
||||
```
|
||||
$ free -t | grep Swap | awk '{printf("Current Swap Utilization is : %.2f%"), $3/$2*100}'
|
||||
Current Swap Utilization is : 0.00%
|
||||
```
|
||||
|
||||
### 方法-1:如何查看Linux下CPU的占用率?
|
||||
|
||||
我们可以使用如下命令的组合来达到此目的。在这种方法中,我们使用`top`,`print`和`awk`命令的组合来获取CPU的占用率。
|
||||
|
||||
如果你正在寻找其他有关于CPU(译者勘误,原文为memory)的文章,你可以导航至如下链接。这些文章有 **[top命令][9]** , **[htop命令][10]** , **[atop命令][11]** 及 **[Glances命令][12]**.
|
||||
|
||||
如果在输出中展示的是多个CPU的情况,那么你需要使用下面的方法。
|
||||
|
||||
```
|
||||
$ top -b -n1 | grep ^%Cpu
|
||||
%Cpu0 : 5.3 us, 0.0 sy, 0.0 ni, 94.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
|
||||
%Cpu1 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
|
||||
%Cpu2 : 0.0 us, 0.0 sy, 0.0 ni, 94.7 id, 0.0 wa, 0.0 hi, 5.3 si, 0.0 st
|
||||
%Cpu3 : 5.3 us, 0.0 sy, 0.0 ni, 94.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
|
||||
%Cpu4 : 10.5 us, 15.8 sy, 0.0 ni, 73.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
|
||||
%Cpu5 : 0.0 us, 5.0 sy, 0.0 ni, 95.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
|
||||
%Cpu6 : 5.3 us, 0.0 sy, 0.0 ni, 94.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
|
||||
%Cpu7 : 5.3 us, 0.0 sy, 0.0 ni, 94.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
|
||||
```
|
||||
|
||||
对于获取不包含百分比符号的`CPU`占用率:
|
||||
|
||||
```
|
||||
$ top -b -n1 | grep ^%Cpu | awk '{cpu+=$9}END{print "Current CPU Utilization is : " 100-cpu/NR}'
|
||||
Current CPU Utilization is : 21.05
|
||||
```
|
||||
|
||||
对于获取包含百分比符号及保留2位小数的`CPU`占用率:
|
||||
|
||||
```
|
||||
$ top -b -n1 | grep ^%Cpu | awk '{cpu+=$9}END{printf("Current CPU Utilization is : %.2f%"), 100-cpu/NR}'
|
||||
Current CPU Utilization is : 14.81%
|
||||
```
|
||||
|
||||
### 方法-2:如何查看Linux下CPU的占用率?
|
||||
|
||||
我们可以使用如下命令的组合来达到此目的。在这种方法中,我们使用的是`top`,`print/printf`和`awk`命令的组合来获取CPU的占用率。
|
||||
|
||||
如果在单个输出中一起展示了所有的CPU的情况,那么你需要使用下面的方法。
|
||||
|
||||
```
|
||||
$ top -b -n1 | grep ^%Cpu
|
||||
%Cpu(s): 15.3 us, 7.2 sy, 0.8 ni, 69.0 id, 6.7 wa, 0.0 hi, 1.0 si, 0.0 st
|
||||
```
|
||||
|
||||
对于获取不包含百分比符号的`CPU`占用率:
|
||||
|
||||
```
|
||||
$ top -b -n1 | grep ^%Cpu | awk '{print "Current CPU Utilization is : " 100-$8}'
|
||||
Current CPU Utilization is : 5.6
|
||||
```
|
||||
|
||||
对于获取包含百分比符号及保留2位小数的`CPU`占用率:
|
||||
|
||||
```
|
||||
$ top -b -n1 | grep ^%Cpu | awk '{printf("Current CPU Utilization is : %.2f%"), 100-$8}'
|
||||
Current CPU Utilization is : 5.40%
|
||||
```
|
||||
|
||||
如下是一些细节:
|
||||
|
||||
* **`top:`** top命令是一种用于查看当前Linux系统下正在运行的进程的非常好的命令。
|
||||
* **`-b:`** -b选项,允许top命令切换至批处理的模式。当你从本地系统运行top命令至远程系统时,它将会非常有用。
|
||||
* **`-n1:`** 迭代次数
|
||||
* **`^%Cpu:`** 过滤以%CPU开头的行。
|
||||
* **`awk:`** awk是一种专门用来做文本数据处理的强大命令。
|
||||
* **`cpu+=$9:`** 对于每一行,将第9列添加至变量‘cpu'。
|
||||
* **`printf:`** 该命令用于格式化和打印数据。
|
||||
* **`%.2f%:`** 默认情况下,它打印小数点后保留6位的浮点数。使用后跟的格式来限制小数位数。
|
||||
* **`100-cpu/NR:`** 最终打印出’CPU平均占用‘,即用100减去其并除以行数。
|
||||
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.2daygeek.com/linux-check-cpu-memory-swap-utilization-percentage/
|
||||
|
||||
作者:[Vinoth Kumar][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[An-DJ](https://github.com/An-DJ)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.2daygeek.com/author/vinoth/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.2daygeek.com/free-command-to-check-memory-usage-statistics-in-linux/
|
||||
[2]: https://www.2daygeek.com/smem-linux-memory-usage-statistics-reporting-tool/
|
||||
[3]: https://www.2daygeek.com/ps_mem-report-core-memory-usage-accurately-in-linux/
|
||||
[4]: https://www.2daygeek.com/linux-vmstat-command-examples-tool-report-virtual-memory-statistics/
|
||||
[5]: https://www.2daygeek.com/easy-ways-to-check-size-of-physical-memory-ram-in-linux/
|
||||
[6]: https://www.2daygeek.com/how-to-create-extend-swap-partition-in-linux-using-lvm/
|
||||
[7]: https://www.2daygeek.com/add-extend-increase-swap-space-memory-file-partition-linux/
|
||||
[8]: https://www.2daygeek.com/shell-script-create-add-extend-swap-space-linux/
|
||||
[9]: https://www.2daygeek.com/linux-top-command-linux-system-performance-monitoring-tool/
|
||||
[10]: https://www.2daygeek.com/linux-htop-command-linux-system-performance-resource-monitoring-tool/
|
||||
[11]: https://www.2daygeek.com/atop-system-process-performance-monitoring-tool/
|
||||
[12]: https://www.2daygeek.com/install-glances-advanced-real-time-linux-system-performance-monitoring-tool-on-centos-fedora-ubuntu-debian-opensuse-arch-linux/
|
||||
Reference in New Issue
Block a user