Akawa

ETY001的博客

今天线上遇到一个bug无法解决,于是去查Log,发现Thinkphp的Log设置的全开,很详细。
但是里面掺杂着很多无用的信息,于是需要写个脚本提取下,也为了日后高效处理日志。

脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
read -p "Enter Filename: " filename
read -p "Enter Output Filename: " outputfilename
awk 'BEGIN{
linestart=1;
lineend=0;
iserr=0;
total=0;
}
{
arr[NR]=$0;
if($0 ~/ERR/){
iserr=1;
}
if($9 ~/^###/){
lineend=NR-1;
if(lineend!=0){
if(iserr==1){
for(i=linestart;i<=lineend;i++){
print arr[i];
delete arr[i];
}
print "";
total++;
iserr=0;
} else {
for(i=linestart;i<=lineend;i++){
delete arr[i];
}
}
linestart=NR;
}
}
}
END{
if(iserr==1){
for(i=linestart;i<=NR;i++){
print arr[i];
delete arr[i];
}
print "";
total++;
} else {
for(i=linestart;i<=NR;i++){
delete arr[i];
}
}
print "Total ERR:",total;
}' $filename > $outputfilename

样例数据:

点击这里下载样例数据

2019-11-09 补充
websocket 的反代

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

upstream websocket {
server 172.20.0.1:8090;
}

server {
listen 443 ssl;
server_name test.com;
ssl_certificate /etc/nginx/ssl/test.com.fullchain.cer;
ssl_certificate_key /etc/nginx/ssl/test.com.key;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
location / {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}

下面是简单的一个nginx反向代理的配置,只需要替换掉proxy_pass中的配置内容即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
server
{
listen 80;
server_name fuckspam.in;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.10.1:3000;
}
}

这两天在写第三方登录的SDK,其中遇到了一个有意思的问题,就是用file_get_content去读取github的接口的时候,
发现一直是false,而浏览器访问则没有问题。经过搜索,发现需要设置下user-agent,只要在php.ini中打开user-agent,
即可正常返回数据了。

这算是file_put_content的高级用法了吧~

1
2
3
4
5
6
7
8
9
10
11
$params     = array(
'access_token' => $access_token
);
$options = array(
'http' => array(
'method' => 'POST',
'content' => http_build_query($params)
)
);
$context = stream_context_create($options);
$r = file_get_contents($url, false, $context);

这两天在写一个python脚本,以实现把数据从多个excel导入进一个mdb中,遇到了比较纠结的问题,

下面的代码是我把问题简化后的代码:

1
2
test = u'测试'
print(str(test))

执行后的报错信息如下:

1
2
3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

在python下两者的转化需要使用decode和encode,具体情况如下:

1
2
3
4
5
a = '测试' # str类型
b = u'测试' # unicode类型

a.decode() # 把str类型解码,即为unicode类型
b.encode('utf-8') # 把unicode类型编码,即为str类型

每次装完树莓派上的系统,第一件事就是要把没有利用的SD卡空间利用上,因为img镜像一般都是最小化的,所以用dd还原镜像到SD卡后,SD卡都还会有一部分未使用的空间。
之前我一直都是装完系统,把SD卡笔记本上的gparted这种图形界面的软件进行扩容操作,由于现在笔记本上没有Archlinux系统了,所以这次就直接在树莓派上操作了。

[root@alarmpi ~]# fdisk /dev/mmcblk0

Welcome to fdisk (util-linux 2.25.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/mmcblk0: 14.9 GiB, 15931539456 bytes, 31116288 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0e8c6e83

Device         Boot  Start     End Sectors  Size Id Type
/dev/mmcblk0p1        2048  206847  204800  100M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      206848 2097151 1890304  923M 83 Linux

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

注意第二个分区的Start,记录下来。然后删掉第二个分区。

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (206848-31116287, default 206848):
Last sector, +sectors or +size{K,M,G,T,P} (206848-31116287, default 31116287):

Created a new partition 2 of type 'Linux' and of size 14.8 GiB.

Command (m for help): p
Disk /dev/mmcblk0: 14.9 GiB, 15931539456 bytes, 31116288 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0e8c6e83

Device         Boot  Start      End  Sectors  Size Id Type
/dev/mmcblk0p1        2048   206847   204800  100M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      206848 31116287 30909440 14.8G 83 Linux

创建个新的主分区,First sector输入第一步记录下来的那个值,也就是我这里的Default的值。新建好后,输入p查看,第二个分区的size已经调整完毕。

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

[root@alarmpi ~]# reboot

输入w,保存分区表并退出,会提示分区表写入失败,因为设备正忙,直接重启即可。

[root@alarmpi ~]# resize2fs /dev/mmcblk0p2
resize2fs 1.42.12 (29-Aug-2014)
Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/mmcblk0p2 is now 3863680 (4k) blocks long.

[root@alarmpi ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        15G  647M   14G   5% /
devtmpfs        214M     0  214M   0% /dev
tmpfs           218M     0  218M   0% /dev/shm
tmpfs           218M  260K  218M   1% /run
tmpfs           218M     0  218M   0% /sys/fs/cgroup
tmpfs           218M     0  218M   0% /tmp
/dev/mmcblk0p1  100M   17M   84M  17% /boot
tmpfs            44M     0   44M   0% /run/user/0

重启后,立即执行resize2fs命令,df查看容量,Avail已经是14G啦,搞定,收工。

码字不易,见好就分享~

CentOS mini下面是没有fat格式的,如果要是想格式化fat格式,所以需要安装下,fat在dosfstools包里面。

yum install dosfstools

今天想用树莓派的时候,发现树莓派的ArchLinux系统坏了,于是就准备重新下载安装,但是发现官网好像已经不再提供现成的镜像了,需要自己手动制作镜像。。。点击这里查看官方提供的制作方法,下载地址

由于我用的是OSX,而官方给的教程是在Linux下的操作,于是我就开我的虚拟机操作,但是发现读卡器在虚拟机里的识别有些问题,于是相当要自己做个镜像包。

####第一步 建立空的镜像文件

dd if=/dev/zero of=new.img bs=2M count=512

因为ArchLinux本身不大,所以建立个1G的镜像文件足够了。

####第二步 用fdisk对new.img进行分区操作

fdisk new.img
  • 输入n进入建立新分区
  • 输入p,选择分区为主分区
  • 回车,默认1
  • 回车,默认开始扇区位置
  • 输入 +100M (引导分区100M足够)

这时,第一个分区建立完毕,回到主目录,输入t,再输入c,设置引导分区类型为W95 FAT32(LBA)。(刚开始烧了4遍,都引导树莓派失败了,就是因为忘记这一步操作了。。

  • 再输入n进入建立新分区
  • 输入p,选择分区为主分区
  • 回车,默认2
  • 回车,默认开始扇区位置
  • 回车,默认使用剩下所有空间。

这样两个分区搞定,输入w,保存分区表,并退出fdisk。

####第三步 挂载new.img成为设备

kpartx -av new.img

之前的两步都是我会的,但是第三步,我着实的折腾了一番,只是知道可以把img挂成设备用,但是不知道怎么搞,从网上搜索的结果,第一个搜索的是用losetup命令,但是出现的问题是,losetup是可以把镜像文件挂载为/dev/loop0,但是里面的分区一直在/dev中找不到。后来搜索 如何挂载虚拟机镜像 这个关键词,才找到了这条命令。这条命令执行后,会有以下设备出现

  • /dev/loop0
  • /dev/mapper/loop0p1
  • /dev/mapper/loop0p2

####第四步 格式化,挂载,并写入文件

mkfs.vfat /dev/mapper/loop0p1
mkdir /mnt/boot
mount /dev/mapper/loop0p1 /mnt/boot
mkfs.ext4 /dev/mapper/loop0p2
mkdir /mnt/root
mount /dev/mapper/loop0p2 /mnt/root

#去官网下载ARMv6 Raspberry Pi这个包
#下载后的文件是ArchLinuxARM-rpi-latest.tar.gz

#bsdtar 可能需要自己安装,yum install bsdtar即可
bsdtar -xpf ArchLinuxARM-rpi-latest.tar.gz -C /mnt/root/

#强制缓存写入
sync

#引导文件
mv /mnt/root/boot/* /mnt/boot/

#卸载
umount /mnt/boot/ /mnt/root/
kpartx -d new.img

这样一个新镜像就做好了,把new.img从虚拟机传出来,用dd直接写入SD卡,成功启动树莓派!

附带两个国内的ArchLinux ARM 的源:

#中科大
Server = http://mirrors.ustc.edu.cn/archlinuxarm/$arch/$repo
#清华
Server = http://mirrors.tuna.tsinghua.edu.cn/archlinuxarm/$arch/$repo

参考文章:Mounting a KVM disk image without KVM

码字不易,见好就分享~

今天遇到了一个bootstrap的问题,就是modal打开后,自动又关闭了,感觉像是toggle方法被触发了两次的感觉,
最终查到原因是因为modal的js代码加载了两次,去掉多加载的那一遍即可解决问题。

需要做一个页面让背景图片能全屏平铺,对于现代浏览器来说很简单,使用下面的代码即可:

1
2
3
4
5
6
7
body{
background:url(../../img/user/user_bg_2.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-c-background-size: cover;
background-size: cover;
}

这个方式可以兼容的浏览器有

* Safari 3+
* Chrome
* IE 9+
* Opera 10+ (Opera 9.5 支持background-size属性 但是不支持cover)
* Firefox 3.6+

但是蛋疼的IE浏览器,就是不给你面子,当你的背景图片宽度小于当前的浏览器宽度的时候,就会有留白,
那么就需要再加上两句,如下

1
2
3
4
5
6
7
8
9
10
body{
background:url(../../img/user/user_bg_2.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-c-background-size: cover;
background-size: cover;

filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/res/img/user/user_bg_2.jpg', sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/res/img/user/user_bg_2.jpg', sizingMethod='scale')";
}

需要注意的是,需要使用绝对路径。用滤镜硬拉伸,可能需要图片做点高斯模糊,要不然效果会很糟糕。

另外还有用img标签来实现的,可以参照这篇文章

0%