first commit
This commit is contained in:
commit
01e8b33396
25
Linux读取或修改系统时间/Makefile
Normal file
25
Linux读取或修改系统时间/Makefile
Normal file
@ -0,0 +1,25 @@
|
||||
SRC := #
|
||||
SRC += main.c
|
||||
|
||||
#OBJ := $(subst .c,.o,$(SRC))
|
||||
OBJ = $(SRC:%.c=%.o)
|
||||
|
||||
CC = gcc
|
||||
FLAG = -Wall
|
||||
OPTION = -lpthread -ldl
|
||||
EXEC_NAME = demo
|
||||
EXEC_PATH = .
|
||||
|
||||
.PHONY:clean demo
|
||||
|
||||
demo:$(OBJ)
|
||||
@echo make ...
|
||||
$(CC) $^ -o $(EXEC_PATH)/$(EXEC_NAME) $(FLAG) $(OPTION)
|
||||
@echo make over
|
||||
@echo Execute target is $(EXEC_PATH)/$(EXEC_NAME)
|
||||
$(OBJ):%.o:%.c
|
||||
$(CC) -c -o $@ $< $(FLAG)
|
||||
clean:
|
||||
@echo clean ...
|
||||
rm $(EXEC_PATH)/$(EXEC_NAME) *.o -rf
|
||||
@echo clean over
|
BIN
Linux读取或修改系统时间/demo
Normal file
BIN
Linux读取或修改系统时间/demo
Normal file
Binary file not shown.
54
Linux读取或修改系统时间/main.c
Normal file
54
Linux读取或修改系统时间/main.c
Normal file
@ -0,0 +1,54 @@
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
// 时间结构体
|
||||
struct tm *local_time; // time to show or modify
|
||||
|
||||
void settime(int tm_hour, int tm_min, int tm_sec)
|
||||
{
|
||||
struct tm *time_set = NULL;
|
||||
struct timeval tv;
|
||||
struct timezone tz;
|
||||
|
||||
/* 获取当前时间 */
|
||||
gettimeofday(&tv, &tz);
|
||||
/* 获取当前时间 */
|
||||
time_set = gmtime(&tv.tv_sec);
|
||||
/* 设置当前时间结构体 */
|
||||
time_set->tm_hour = tm_hour;
|
||||
time_set->tm_min = tm_min;
|
||||
time_set->tm_sec = tm_sec;
|
||||
/* 获取用秒表示的时间 */
|
||||
tv.tv_sec = mktime(time_set);
|
||||
/* 设置当前时间 */
|
||||
settimeofday(&tv, &tz);
|
||||
}
|
||||
|
||||
void refresh_clock(void)
|
||||
{
|
||||
time_t timep;
|
||||
|
||||
time(&timep);
|
||||
local_time = localtime(&timep); // 读取系统时间
|
||||
|
||||
// 时分秒
|
||||
int hour = local_time->tm_hour;
|
||||
int minute = local_time->tm_min;
|
||||
int second = local_time->tm_sec;
|
||||
printf("h = %d, m = %d, s = %d\n", hour, minute, second);
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
printf("修改前的时间:\n");
|
||||
refresh_clock();
|
||||
settime(11, 34, 50);
|
||||
|
||||
printf("修改后的时间:\n");
|
||||
refresh_clock();
|
||||
return 0;
|
||||
}
|
BIN
Linux读取或修改系统时间/main.o
Normal file
BIN
Linux读取或修改系统时间/main.o
Normal file
Binary file not shown.
1
Linux读取或修改系统时间/readme.txt
Normal file
1
Linux读取或修改系统时间/readme.txt
Normal file
@ -0,0 +1 @@
|
||||
修改系统时间需要root权限,所以用sudo运行程序
|
BIN
gtk透明列表/exe
Executable file
BIN
gtk透明列表/exe
Executable file
Binary file not shown.
142
gtk透明列表/list.c
Normal file
142
gtk透明列表/list.c
Normal file
@ -0,0 +1,142 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "list.h"
|
||||
|
||||
//列表滑动、选中控制/*{{{*/
|
||||
static int press_y,previous_y,current_y; //滑动控制变量
|
||||
gboolean deal_press_event(GtkWidget *widget,GdkEventButton *event,gpointer data) //按下
|
||||
{
|
||||
GdkColor color;
|
||||
LIST *list=(LIST *)data;
|
||||
if(GDK_2BUTTON_PRESS == event->type) //双击选中
|
||||
{
|
||||
gdk_color_parse(NORMAL_COLOR, &color);
|
||||
gtk_widget_modify_fg(list->label[list->tab_select], GTK_STATE_NORMAL, &color);
|
||||
list->tab_select=(int)(event->y-current_y)/(list->tab_height);
|
||||
gdk_color_parse(HIGH_COLOR, &color);
|
||||
gtk_widget_modify_fg(list->label[list->tab_select], GTK_STATE_NORMAL, &color);
|
||||
list_select_back(list); //双击事件回调,由外部实现
|
||||
}
|
||||
press_y=event->y;
|
||||
previous_y=current_y;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean deal_motion_event(GtkWidget *widget,GdkEventMotion *event,gpointer data) //拖动
|
||||
{
|
||||
LIST *list=(LIST *)data;
|
||||
current_y=previous_y+event->y-press_y;
|
||||
gtk_fixed_move(GTK_FIXED(list->fixed_bottom),list->fixed_top,0,current_y);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean deal_release_event(GtkWidget *widget,GdkEventButton *event,gpointer data) //释放
|
||||
{
|
||||
LIST *list=(LIST *)data;
|
||||
if((list->tab_show*list->tab_height >= (list->total)*(list->tab_height)) || (current_y >=0))
|
||||
current_y=0;
|
||||
else if(list->tab_show*list->tab_height > current_y+(list->total)*(list->tab_height))
|
||||
current_y=list->tab_show*list->tab_height-(list->total)*(list->tab_height);
|
||||
else;
|
||||
gtk_fixed_move(GTK_FIXED(list->fixed_bottom),list->fixed_top,0,current_y);
|
||||
return TRUE;
|
||||
}/*}}}*/
|
||||
|
||||
LIST *cgtk_list_new(char **name,int width,int height,int show,gpointer data) //创建列表/*{{{*/
|
||||
{
|
||||
int i;
|
||||
GdkColor color;
|
||||
LIST *list=(LIST *)calloc(1,sizeof(LIST)); //分配列表空间
|
||||
list->label=(GtkWidget **)calloc(0,sizeof(GtkWidget *)); //分配列表行空间,初始为0
|
||||
list->tab_show=show;
|
||||
list->tab_width=width;
|
||||
list->tab_height=height;
|
||||
list->out=data;
|
||||
list->event_box=gtk_event_box_new();
|
||||
gtk_widget_set_size_request(list->fixed_bottom,width,show*height);
|
||||
list->fixed_bottom=gtk_fixed_new();
|
||||
gtk_widget_set_size_request(list->fixed_bottom,width,show*height);
|
||||
list->fixed_top=gtk_fixed_new();
|
||||
//gtk_widget_set_size_request(list->fixed_bottom,width,show*height);
|
||||
gtk_container_add(GTK_CONTAINER(list->event_box),list->fixed_bottom);
|
||||
gtk_fixed_put(GTK_FIXED(list->fixed_bottom),list->fixed_top,0,0);
|
||||
|
||||
g_signal_connect(GTK_OBJECT(list->event_box), "button_press_event", G_CALLBACK(deal_press_event),list);
|
||||
g_signal_connect(GTK_OBJECT(list->event_box), "motion_notify_event", G_CALLBACK(deal_motion_event),list);
|
||||
g_signal_connect(GTK_OBJECT(list->event_box), "button_release_event", G_CALLBACK(deal_release_event),list);
|
||||
|
||||
gdk_color_parse(NORMAL_COLOR, &color);
|
||||
for(i=0;NULL != name[i];i++)
|
||||
{
|
||||
if(NULL == (list->label=realloc(list->label,(++(list->total))*sizeof(GtkWidget *))))
|
||||
{
|
||||
printf("\n>>>Can not get more memory space!!!<<<\n");
|
||||
return NULL;
|
||||
}
|
||||
list->label[i]=gtk_label_new(name[i]);
|
||||
gtk_widget_modify_fg(list->label[i], GTK_STATE_NORMAL, &color);
|
||||
gtk_widget_set_size_request(list->label[i],width,height);
|
||||
gtk_fixed_put(GTK_FIXED(list->fixed_top),list->label[i],0,height*i);
|
||||
}
|
||||
return list;
|
||||
}/*}}}*/
|
||||
|
||||
int cgtk_list_refresh(LIST *list,char **name) //刷新列表/*{{{*/
|
||||
{
|
||||
int i;
|
||||
GdkColor color;
|
||||
for(i=0;i<list->total;i++)
|
||||
gtk_widget_destroy(list->label[i]);
|
||||
list->total=0;
|
||||
free(list->label);
|
||||
list->label=(GtkWidget **)calloc(0,sizeof(GtkWidget *)); //分配列表行空间,初始为0
|
||||
gdk_color_parse(NORMAL_COLOR, &color);
|
||||
for(i=0;NULL != name[i];i++)
|
||||
{
|
||||
if(NULL == (list->label=realloc(list->label,(++(list->total))*sizeof(GtkWidget *))))
|
||||
{
|
||||
printf("\n>>>Can not get more memory space!!!<<<\n");
|
||||
return -2;
|
||||
}
|
||||
list->label[i]=gtk_label_new(name[i]);
|
||||
gtk_widget_modify_fg(list->label[i], GTK_STATE_NORMAL, &color);
|
||||
gtk_widget_set_size_request(list->label[i],list->tab_width,list->tab_height);
|
||||
gtk_fixed_put(GTK_FIXED(list->fixed_top),list->label[i],0,list->tab_height*i);
|
||||
gtk_widget_show(list->label[i]);
|
||||
}
|
||||
return 0;
|
||||
}/*}}}*/
|
||||
|
||||
int cgtk_list_set_select_num(LIST *list,int row_num) //修改选中行行号/*{{{*/
|
||||
{
|
||||
if(0>row_num || list->total<=row_num)
|
||||
{
|
||||
printf("\n>>>Set Wrong row_num !!! <<<\n");
|
||||
return -1;
|
||||
}
|
||||
GdkColor color;
|
||||
gdk_color_parse(NORMAL_COLOR, &color);
|
||||
gtk_widget_modify_fg(list->label[list->tab_select], GTK_STATE_NORMAL, &color);
|
||||
list->tab_select=row_num;
|
||||
gdk_color_parse(HIGH_COLOR, &color);
|
||||
gtk_widget_modify_fg(list->label[list->tab_select], GTK_STATE_NORMAL, &color);
|
||||
return 0;
|
||||
}/*}}}*/
|
||||
|
||||
int cgtk_list_get_select_num(LIST *list) //获取选中行行号/*{{{*/
|
||||
{
|
||||
return list->tab_select;
|
||||
}/*}}}*/
|
||||
|
||||
char *cgtk_list_get_select_text(LIST *list) //获取选中行文本/*{{{*/
|
||||
{
|
||||
char *text=(char *)gtk_label_get_text(GTK_LABEL(list->label[list->tab_select]));
|
||||
return text;
|
||||
}/*}}}*/
|
||||
|
||||
void cgtk_list_destroy(LIST *list) //释放列表占用的空间/*{{{*/
|
||||
{
|
||||
free(list->label);
|
||||
free(list);
|
||||
}/*}}}*/
|
||||
|
30
gtk透明列表/list.h
Normal file
30
gtk透明列表/list.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef __COLBEN_LIST__
|
||||
#define __COLBEN_LIST__
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
#define NORMAL_COLOR "black" //非选中颜色
|
||||
#define HIGH_COLOR "white" //选中颜色
|
||||
|
||||
typedef struct //list句柄
|
||||
{
|
||||
gint total; //标签总数量
|
||||
gint tab_show; //标签显示数量
|
||||
gint tab_width; //标签宽度
|
||||
gint tab_height; //标签高度
|
||||
gint tab_select; //当前选中标签
|
||||
gpointer out; //双击事件回调参数
|
||||
GtkWidget *event_box; //底层事件盒子,需手动添加到自定义布局中
|
||||
GtkWidget *fixed_bottom; //中层固定布局
|
||||
GtkWidget *fixed_top; //上层固定布局
|
||||
GtkWidget **label; //每一行的信息
|
||||
}LIST;
|
||||
|
||||
extern LIST *cgtk_list_new(char **,int,int,int,gpointer); //创建列表,需指定指针数组,列表宽度,单行高度,双击回调参数指针
|
||||
extern int cgtk_list_refresh(LIST *,char **); //刷新列表,需指定列表和新指针数组,双击回调参数指针
|
||||
extern int cgtk_list_set_select_num(LIST *,int); //设置选中行号
|
||||
extern int cgtk_list_get_select_num(LIST *); //获取选中行号
|
||||
extern char *cgtk_list_get_select_text(LIST *); //获取选中行文本
|
||||
extern void cgtk_list_destroy(LIST *); //释放列表内存空间
|
||||
extern void list_select_back(gpointer); //双击事件回调,需外部实现
|
||||
|
||||
#endif
|
46
gtk透明列表/main.c
Normal file
46
gtk透明列表/main.c
Normal file
@ -0,0 +1,46 @@
|
||||
#include <gtk/gtk.h>
|
||||
#include <glade/glade.h>
|
||||
#include "list.h"
|
||||
|
||||
char *name1[]={"Colben","Shelly","Saferin","Ada","Obo","Kathy",NULL}; //之前
|
||||
char *name2[]={"Colben","Shelly","Bather","Ada","Obo","Kathy","Jathy","Rassol","Colben","Shelly","Bather","Rassol","Colben","Shelly","Bather","Ada","Obo","Kathy",NULL}; //之后
|
||||
|
||||
void list_select_back(gpointer data) //双击选中回调
|
||||
{
|
||||
LIST *list=(LIST *)data;
|
||||
gtk_label_set_text(GTK_LABEL(list->out),cgtk_list_get_select_text(list));
|
||||
printf("\n>>>>>>----------%d----------<<<<<<\n",cgtk_list_get_select_num(list));
|
||||
printf("\n>>>>>>----------%s----------<<<<<<\n",cgtk_list_get_select_text(list));
|
||||
}
|
||||
|
||||
void button_refresh_list(GtkWidget *widget,gpointer data) //按钮单击回调
|
||||
{
|
||||
LIST *list=(LIST *)data;
|
||||
cgtk_list_refresh(list,name2);
|
||||
}
|
||||
|
||||
int main(int argc,char *argv[])
|
||||
{
|
||||
gtk_init(&argc,&argv);
|
||||
GladeXML *gxml=glade_xml_new("test.glade",NULL,NULL);
|
||||
GtkWidget *main_window=glade_xml_get_widget(gxml,"window1");
|
||||
g_signal_connect(main_window,"destroy",G_CALLBACK(gtk_main_quit),NULL);
|
||||
|
||||
GtkWidget *fixed=glade_xml_get_widget(gxml,"fixed1");
|
||||
GtkWidget *label=glade_xml_get_widget(gxml,"label1");
|
||||
|
||||
LIST *list=cgtk_list_new(name1,240,40,8,label); //创建列表,指针即可,无需分配空间
|
||||
if(NULL == list)
|
||||
{
|
||||
printf("\n>>>>>> !!! Memory not enough !!! <<<<<<\n");
|
||||
return -1;
|
||||
}
|
||||
gtk_fixed_put(GTK_FIXED(fixed),list->event_box,500,80); //把列表添加到主窗口的固定布局中
|
||||
|
||||
GtkWidget *button=glade_xml_get_widget(gxml,"button1");
|
||||
g_signal_connect(button,"clicked",G_CALLBACK(button_refresh_list),list); //按钮信号注册
|
||||
|
||||
gtk_widget_show_all(main_window);
|
||||
gtk_main();
|
||||
return 0;
|
||||
}
|
39
gtk透明列表/makefile
Normal file
39
gtk透明列表/makefile
Normal file
@ -0,0 +1,39 @@
|
||||
#==============================================
|
||||
# 文件名:makefile
|
||||
# 作者: 巴拉克-侯赛因-奥巴马
|
||||
# 创建时间:2014-05-23 00:21:34
|
||||
# 地址: 美国华盛顿市区中心宾西法尼亚大街1600号
|
||||
#==============================================
|
||||
|
||||
cc=gcc
|
||||
|
||||
target=exe
|
||||
|
||||
object_h=list.h
|
||||
|
||||
object_o=main.o list.o
|
||||
|
||||
flags=`pkg-config --cflags --libs gtk+-2.0 libglade-2.0`
|
||||
|
||||
$(target): $(object_o)
|
||||
@$(cc) -o $@ $^ $(flags)
|
||||
@echo "成功生成可执行文件: $@ !!"
|
||||
@echo '^_^ ^_^ ^_^'
|
||||
|
||||
%.o: %.c $(object_h)
|
||||
@$(cc) -o $@ -c $< $(flags)
|
||||
@echo '^_^'
|
||||
@echo "成功编译 $< 生成 $@ 文件!!"
|
||||
|
||||
clean:
|
||||
@rm -rf $(object_o)
|
||||
@echo '^_^'
|
||||
@echo "成功删除所有目标文件!!"
|
||||
@echo '^_^'
|
||||
|
||||
clear:
|
||||
@rm -rf $(object_o) $(target)
|
||||
@echo '^_^'
|
||||
@echo "成功删除所有非源码文件!!"
|
||||
@echo '^_^'
|
||||
|
46
gtk透明列表/test.glade
Normal file
46
gtk透明列表/test.glade
Normal file
@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<glade-interface>
|
||||
<!-- interface-requires gtk+ 2.24 -->
|
||||
<!-- interface-naming-policy project-wide -->
|
||||
<widget class="GtkWindow" id="window1">
|
||||
<property name="width_request">800</property>
|
||||
<property name="height_request">480</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="resizable">False</property>
|
||||
<property name="window_position">center-always</property>
|
||||
<child>
|
||||
<widget class="GtkFixed" id="fixed1">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<child>
|
||||
<widget class="GtkButton" id="button1">
|
||||
<property name="label" translatable="yes">Refresh</property>
|
||||
<property name="width_request">100</property>
|
||||
<property name="height_request">80</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
<property name="use_action_appearance">False</property>
|
||||
</widget>
|
||||
<packing>
|
||||
<property name="x">76</property>
|
||||
<property name="y">295</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<widget class="GtkLabel" id="label1">
|
||||
<property name="width_request">100</property>
|
||||
<property name="height_request">80</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="label" translatable="yes">label</property>
|
||||
</widget>
|
||||
<packing>
|
||||
<property name="x">87</property>
|
||||
<property name="y">74</property>
|
||||
</packing>
|
||||
</child>
|
||||
</widget>
|
||||
</child>
|
||||
</widget>
|
||||
</glade-interface>
|
98
music.py
Normal file
98
music.py
Normal file
@ -0,0 +1,98 @@
|
||||
#!/usr/bin/python
|
||||
# -*- encoding:utf-8 -*-
|
||||
#=========================================
|
||||
# Filename : music.py
|
||||
# Filetype : Python
|
||||
# Author : Colben
|
||||
# Create : 2015-08-04 20:50:17
|
||||
#=========================================
|
||||
|
||||
import sys, os, time, subprocess, re, chardet, urllib2, json, threading
|
||||
|
||||
def load_lrc(lrc_basename):
|
||||
try:
|
||||
lrc_contains = open(lrc_basename+'.lrc', 'rb').read()
|
||||
except:
|
||||
print '\033[4;0H\033[K\t Local lrc not found, checking internet ...'
|
||||
try:
|
||||
lrc_contains = urllib2.urlopen(json.loads(urllib2.urlopen('http://geci.me/api/lyric/'+os.path.split(lrc_basename)[1]).read())['result'][0]['lrc']).read()
|
||||
except Exception, e:
|
||||
print '\033[4;0H\033[K\t Lrc not found ...'
|
||||
return
|
||||
try:
|
||||
lrc_fp.close()
|
||||
except:
|
||||
pass
|
||||
encoding = chardet.detect(lrc_contains)['encoding']
|
||||
if 'utf-8' != encoding:
|
||||
lrc_contains = lrc_contains.decode(encoding).encode('utf-8')
|
||||
for eachline in re.split(r'\n', lrc_contains):
|
||||
line = re.split(r']', eachline)
|
||||
if 1 < len(line):
|
||||
for tm in line[0:-1]:
|
||||
try:
|
||||
time = re.match(r'(\d\d)\s*:\s*(\d\d)',tm.strip(' [')).groups()
|
||||
pos = 60*int(time[0]) + int(time[1])
|
||||
lrc[pos] = line[-1]
|
||||
except Exception, e:
|
||||
pass
|
||||
return
|
||||
|
||||
def main(song):
|
||||
global lrc
|
||||
lrc = {}
|
||||
print '\033[2J\033[2;0H\tPlaying %s ...'%song[:60]
|
||||
try:
|
||||
p = subprocess.Popen('mplayer %s 2>&1'%song, stdout = subprocess.PIPE, shell = True)
|
||||
except Exception, e:
|
||||
print '\033[9;0H\t%s'%e
|
||||
p.kill()
|
||||
return 1
|
||||
|
||||
while True:
|
||||
match = re.match(r'A:.*[\d:.()]* of (\d+)', p.stdout.read(30))
|
||||
if None != p.poll():
|
||||
print '\033[9;0H\tFailed to recognize file format .'
|
||||
return 1
|
||||
if not match:
|
||||
output = p.stdout.readline()
|
||||
else:
|
||||
tot_time = int(match.group(1)) - 1
|
||||
for jump in range(1, 10):
|
||||
if 70 >= tot_time/jump:
|
||||
break
|
||||
#print '\033[15;0H\ttotal time: %d'%tot_time
|
||||
break
|
||||
print '\033[?25l'
|
||||
thread_load_lrc = threading.Thread(target = load_lrc, args = (os.path.splitext(song)[0], ))
|
||||
thread_load_lrc.start()
|
||||
while True:
|
||||
cur_char = p.stdout.read(1)
|
||||
if 'A' == cur_char:
|
||||
try:
|
||||
cur_time = int(p.stdout.read(5)[1:])
|
||||
print '\033[6;0H\033[K\tCurrent %d / Total %d'%(cur_time, tot_time)
|
||||
print '\033[1;0H', '-'*(tot_time/jump)
|
||||
print '\033[1;0H', '='*(cur_time/jump)
|
||||
print '\033[7;0H', '-'*(tot_time/jump)
|
||||
print '\033[7;0H', '='*(cur_time/jump)
|
||||
if tot_time <= cur_time:
|
||||
print '\033[8;0H\033[K\tquit'
|
||||
break
|
||||
print '\033[4;0H\033[K\t%s'%lrc[cur_time]
|
||||
except Exception, e:
|
||||
#print '\033[17;0H\tERROR:', e
|
||||
pass
|
||||
elif 'E' == cur_char:
|
||||
print '\033[8;0H\033[K\tquit'
|
||||
break
|
||||
|
||||
p.wait()
|
||||
print '\033[10;0H\033[?25h'
|
||||
return 0
|
||||
|
||||
if '__main__' == __name__:
|
||||
if 2 != len(sys.argv) or not os.path.isfile(sys.argv[1]):
|
||||
print '\nUsage:', sys.argv[0], '{exist music filename}\n'
|
||||
else:
|
||||
main(sys.argv[1])
|
64
python进程池.py
Normal file
64
python进程池.py
Normal file
@ -0,0 +1,64 @@
|
||||
#!/usr/bin/python2
|
||||
# -*- coding:utf-8 -*-
|
||||
#=========================================
|
||||
# Filename : process_pool.py
|
||||
# Author : Colben
|
||||
# Create : 2018-06-25 16:09
|
||||
#=========================================
|
||||
|
||||
from multiprocessing import Process
|
||||
|
||||
# 进程池容量,即同时运行的最多任务数量
|
||||
POOL_SIZE = 10
|
||||
|
||||
# 初始化任务列表,这里用字典存储,key是任务,value是子进程
|
||||
PROCESS_LIST = {}
|
||||
|
||||
# 执行一个任务
|
||||
def exec_task():
|
||||
#print task, '\033[32mbegan ...\033[0m'
|
||||
pass
|
||||
|
||||
# 检查任务列表情况
|
||||
# empty = False 用于查看列表是否已满(达到10个)
|
||||
# empty = True 用于查看列表是否已空(0个)
|
||||
def check_pool_state(empty=False):
|
||||
while True:
|
||||
print "\033[2J\033[0;0H"
|
||||
for task, process in PROCESS_LIST.items():
|
||||
if process.is_alive():
|
||||
print 'Handling', task, '...'
|
||||
else:
|
||||
#print task, '\033[32mfinished\033[0m'
|
||||
PROCESS_LIST.pop(task)
|
||||
if not empty and POOL_SIZE > len(PROCESS_LIST): break
|
||||
if empty and 0 == len(PROCESS_LIST):break
|
||||
sleep(0.5)
|
||||
return
|
||||
|
||||
# 批量启动任务
|
||||
def start_process_pool(tasks):
|
||||
for task in tasks:
|
||||
sub_process = Process(target=exec_task, args=(task,))
|
||||
# 启动子进程
|
||||
sub_process.start()
|
||||
# 记录子进程信息到任务列表
|
||||
PROCESS_LIST[task] = sub_process
|
||||
# 查看列表是否已满
|
||||
check_pool_state()
|
||||
# 查看列表是否已空
|
||||
check_pool_state(empty=True)
|
||||
return
|
||||
|
||||
# 测试开始 --------
|
||||
def main():
|
||||
tasks = [6,7,8,9]
|
||||
# 重写上面的 exec_task 函数,实现阶乘
|
||||
start_process_pool(tasks)
|
||||
return
|
||||
|
||||
if '__main__' == __name__:
|
||||
main()
|
||||
# 测试结束 --------
|
||||
|
||||
|
79
shell数组实现进程池.sh
Normal file
79
shell数组实现进程池.sh
Normal file
@ -0,0 +1,79 @@
|
||||
#!/bin/bash
|
||||
#=========================================
|
||||
# Filename : process_pool.sh
|
||||
# Author : Colben
|
||||
# Create : 2018-06-16 11:56
|
||||
#=========================================
|
||||
|
||||
#set -euo pipefail
|
||||
export LANG=en_US.UTF-8
|
||||
#[ 0 = $UID ] || exit 1
|
||||
mkdir -p /tmp/agent/results || exit 1
|
||||
cd /tmp/agent && rm -f results/* || exit 1
|
||||
# 进程池容量
|
||||
readonly POOL_SIZE=4
|
||||
# 进程池数组,存储子进程 pid
|
||||
POOL=()
|
||||
|
||||
# 功能: 显示执行结果
|
||||
function ShowResult {
|
||||
local status=
|
||||
cd results
|
||||
for task in $(ls); do
|
||||
status=$(sed -n '$p' $task)
|
||||
if [ 0 -eq $status ]; then
|
||||
echo -en "$task > \033[32;1mNORMAL\033[0m > "
|
||||
else
|
||||
echo -en "$task > \033[31;1mERROR\033[0m > "
|
||||
fi
|
||||
sed -n -e '$d' -e '1,$p' $task | tr '\n' ' '
|
||||
echo -e "\033[0m"
|
||||
done
|
||||
}
|
||||
|
||||
# 功能: 执行一个任务
|
||||
# 根据业务需求自己实现
|
||||
function ExecTask {
|
||||
# for 循环模拟一次任务
|
||||
for n in $(seq 1 $1); do
|
||||
echo 'a'
|
||||
sleep 1
|
||||
done &>> results/$1 #记录输出结果
|
||||
# 记录命令返回码
|
||||
echo $? >> results/$1
|
||||
}
|
||||
|
||||
# 功能: 检查进程池状
|
||||
# 参数: 一个非负整数
|
||||
# 参数为 0 时检查进程池是否为空
|
||||
# 参数为正整数是检查进程池是否已满
|
||||
function CheckPool {
|
||||
while :; do
|
||||
clear
|
||||
for pid in ${!POOL[@]}; do
|
||||
if [ -d /proc/$pid ]; then
|
||||
echo "Handling ${POOL[$pid]} ..."
|
||||
else
|
||||
#echo "Finished ${POOL[$pid]}"
|
||||
unset POOL[$pid]
|
||||
fi
|
||||
done
|
||||
if [ 0 -eq $1 ]; then
|
||||
[ 0 -eq ${#POOL[@]} ] && break
|
||||
else
|
||||
[ $POOL_SIZE -gt ${#POOL[@]} ] && break
|
||||
fi
|
||||
sleep 0.5
|
||||
done
|
||||
}
|
||||
|
||||
# 测试开始
|
||||
for task in $(seq 11 18); do # 这里的多任务用几个整数模拟
|
||||
CheckPool $POOL_SIZE # 检查进程池是否已满
|
||||
ExecTask $task & # 后台启动任务
|
||||
POOL[$!]="$task" # 记录任务 pid 到进程池
|
||||
done
|
||||
CheckPool 0 # 检查进程池是否已空
|
||||
ShowResult
|
||||
# 测试结束
|
||||
|
38
shell有名管道实现进程池.sh
Normal file
38
shell有名管道实现进程池.sh
Normal file
@ -0,0 +1,38 @@
|
||||
#!/bin/bash
|
||||
#=========================================
|
||||
# Filename : process_pool.sh
|
||||
# Author : Colben
|
||||
# Create : 2018-12-14 19:04
|
||||
#=========================================
|
||||
|
||||
#set -euo pipefail
|
||||
export LANG=en_US.UTF-8
|
||||
#[ 0 = $UID ] || exit 1
|
||||
# 创建有名管道,模拟 go 的有缓存 channal
|
||||
rm -rf pool.pipe && mkfifo pool.pipe || exit 1
|
||||
exec 1022<> pool.pipe && rm -f pool.pipe
|
||||
|
||||
# 初始向有名管道写入10个字符
|
||||
printf "%10s" '' >&1022
|
||||
|
||||
# 功能: 执行一个任务
|
||||
# 根据业务需求自己实现
|
||||
function ExecTask {
|
||||
local ts=1
|
||||
let "ts=$1%4"
|
||||
sleep $ts
|
||||
}
|
||||
|
||||
# 测试开始
|
||||
for task in $(seq 11 88); do # 这里的多任务用几个整数模拟
|
||||
read -n1 -u1022 # 每次执行任务前读出一个字符
|
||||
(
|
||||
ExecTask $task
|
||||
echo -n '.'
|
||||
echo >&1022 # 每次执行任务后再写入一个字符
|
||||
) & # 后台启动任务
|
||||
done
|
||||
wait
|
||||
echo
|
||||
# 测试结束
|
||||
|
94
光标操作/Print_Esc.c
Normal file
94
光标操作/Print_Esc.c
Normal file
@ -0,0 +1,94 @@
|
||||
#include "Print_Esc.h"
|
||||
|
||||
//清屏
|
||||
void clear_screen(void)
|
||||
{// ESC[2J
|
||||
printf("\033[2J");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
//清除从光标位置到行末的内容
|
||||
void clear_to_end(void)
|
||||
{// ESC[K
|
||||
printf("\033[K");
|
||||
fflush(stdout);
|
||||
|
||||
} //光标移动到(x,y)
|
||||
|
||||
void cusor_moveto(int x, int y)
|
||||
{// ESC[y;xH
|
||||
printf("\033[%d;%dH",y,x);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
//保存光标位置
|
||||
void cusor_get_pos(void)
|
||||
{// ESC[s
|
||||
printf("\033[s");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
//恢复光标位置
|
||||
void cusor_set_pos(void)
|
||||
{// ESC[u
|
||||
printf("\033[u");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
//光标上移num行
|
||||
void cusor_up(int num)
|
||||
{
|
||||
while(num--)
|
||||
{ // up = ESC[A
|
||||
printf("\033[A");
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
//光标下移num行
|
||||
void cusor_down(int num)
|
||||
{
|
||||
while(num--)
|
||||
{// down = ESC[B
|
||||
printf("\033[B");
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
//光标左移num个字符
|
||||
void cusor_lift(int num)
|
||||
{
|
||||
while(num--)
|
||||
{// lift = ESC[D
|
||||
printf("\033[D");
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
//光标右移num个字符
|
||||
void cusor_right(int num)
|
||||
{
|
||||
while(num--)
|
||||
{ // right = ESC[C
|
||||
printf("\033[C");
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
//设置前景颜色
|
||||
void set_fg_color(int color)
|
||||
{// ESC[#m
|
||||
printf("\033[%dm",color);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
//设置背景颜色
|
||||
void set_bg_color(int color)
|
||||
{// ESC[#m
|
||||
printf("\033[%dm",(color+10));
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
34
光标操作/Print_Esc.h
Normal file
34
光标操作/Print_Esc.h
Normal file
@ -0,0 +1,34 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define COLOR_RED 31
|
||||
#define COLOR_BLACK 30
|
||||
#define COLOR_GREEN 32
|
||||
#define COLOR_BLUE 34
|
||||
#define COLOR_YELLOW 33
|
||||
#define COLOR_WHITE 37
|
||||
#define COLOR_CYAN 36
|
||||
#define COLOR_MAGENTA 35
|
||||
|
||||
//清屏
|
||||
void clear_screen(void);
|
||||
//清除从光标位置到行末的内容
|
||||
void clear_to_end(void);
|
||||
//光标移动到(x,y)
|
||||
void cusor_moveto(int x, int y);
|
||||
//保存光标位置
|
||||
void cusor_get_pos(void);
|
||||
//恢复光标位置
|
||||
void cusor_set_pos(void);
|
||||
//光标上移num行
|
||||
void cusor_up(int num);
|
||||
//光标下移num行
|
||||
void cusor_down(int num);
|
||||
//光标左移num个字符
|
||||
void cusor_lift(int num);
|
||||
//光标右移num个字符
|
||||
void cusor_right(int num);
|
||||
//设置前景颜色
|
||||
void set_fg_color(int color);
|
||||
//设置背景颜色
|
||||
void set_bg_color(int color);
|
57
光标操作/console.c
Normal file
57
光标操作/console.c
Normal file
@ -0,0 +1,57 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "console.h"
|
||||
|
||||
void cusor_moveto(int x, int y)
|
||||
{// ESC[y;xH
|
||||
printf("\033[%d;%dH",y,x);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
//保存光标位置
|
||||
void cusor_get_pos(void)
|
||||
{// ESC[s
|
||||
printf("\033[s");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
//恢复光标位置
|
||||
void cusor_set_pos(void)
|
||||
{// ESC[u
|
||||
printf("\033[u");
|
||||
fflush(stdout);
|
||||
}
|
||||
void cusor_hide(void)
|
||||
{
|
||||
printf("\033[?25l");
|
||||
}
|
||||
//清屏
|
||||
void clear_screen(void)
|
||||
{// ESC[2J
|
||||
printf("\033[2J");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
/*
|
||||
COLOR_RED 红
|
||||
COLOR_BLACK 黑
|
||||
COLOR_GREEN 绿
|
||||
COLOR_BLUE 蓝
|
||||
COLOR_YELLOW 黄
|
||||
COLOR_WHITE 白
|
||||
COLOR_CYAN 青
|
||||
COLOR_MAGENTA 洋红
|
||||
*/
|
||||
//设置前景颜色
|
||||
void set_fg_color(int color)
|
||||
{// ESC[#m
|
||||
printf("\033[%dm",color);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
//设置背景颜色
|
||||
void set_bg_color(int color)
|
||||
{// ESC[#m
|
||||
printf("\033[%dm",(color+10));
|
||||
fflush(stdout);
|
||||
}
|
33
光标操作/console.h
Normal file
33
光标操作/console.h
Normal file
@ -0,0 +1,33 @@
|
||||
#ifndef _CONSOLE_H_
|
||||
#define _CONSOLE_H_
|
||||
|
||||
#define COLOR_RED 31
|
||||
#define COLOR_BLACK 30
|
||||
#define COLOR_GREEN 32
|
||||
#define COLOR_BLUE 34
|
||||
#define COLOR_YELLOW 33
|
||||
#define COLOR_WHITE 37
|
||||
#define COLOR_CYAN 36
|
||||
#define COLOR_MAGENTA 35
|
||||
/*
|
||||
COLOR_RED 红
|
||||
COLOR_BLACK 黑
|
||||
COLOR_GREEN 绿
|
||||
COLOR_BLUE 蓝
|
||||
COLOR_YELLOW 黄
|
||||
COLOR_WHITE 白
|
||||
COLOR_CYAN 青
|
||||
COLOR_MAGENTA 洋红
|
||||
*/
|
||||
|
||||
extern void cusor_moveto(int x, int y);//光标跳转到 y行 x列
|
||||
extern void cusor_get_pos(void);//保存光标位置
|
||||
extern void cusor_hide(void);
|
||||
extern void cusor_set_pos(void);//恢复光标位置
|
||||
extern void clear_screen(void);//清屏
|
||||
extern void set_fg_color(int color);//设置字体前景色
|
||||
extern void set_bg_color(int color);//设置字体背景色
|
||||
|
||||
#endif //_CONSOLE_H_
|
||||
|
||||
|
15
光标操作/cursor_demo/Makefile
Normal file
15
光标操作/cursor_demo/Makefile
Normal file
@ -0,0 +1,15 @@
|
||||
CC=gcc
|
||||
obj=console.o test.o
|
||||
target=test
|
||||
CFLAGS=-Wall
|
||||
|
||||
$(target):$(obj)
|
||||
@$(CC) $^ -o $@ $(CFLAGS)
|
||||
test.o:test.c
|
||||
@$(CC) -c $< -o $@ $(CFLAGS)
|
||||
console.o:console.c
|
||||
@$(CC) -c $< -o $@ $(CFLAGS)
|
||||
|
||||
|
||||
clean:
|
||||
@rm $(obj) $(target)
|
63
光标操作/cursor_demo/console.c
Normal file
63
光标操作/cursor_demo/console.c
Normal file
@ -0,0 +1,63 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "console.h"
|
||||
|
||||
void cusor_moveto(int x, int y)
|
||||
{// ESC[y;xH
|
||||
printf("\033[%d;%dH",y,x);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
//保存光标位置
|
||||
void cusor_get_pos(void)
|
||||
{// ESC[s
|
||||
printf("\033[s");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
//恢复光标位置
|
||||
void cusor_set_pos(void)
|
||||
{// ESC[u
|
||||
printf("\033[u");
|
||||
fflush(stdout);
|
||||
}
|
||||
//隐藏光标
|
||||
void cusor_hide(void)
|
||||
{
|
||||
printf("\033[?25l");
|
||||
}
|
||||
//显示光标
|
||||
void cusor_show(void)
|
||||
{
|
||||
printf("\33[?25h");
|
||||
}
|
||||
//清屏
|
||||
void clear_screen(void)
|
||||
{// ESC[2J
|
||||
printf("\033[2J");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
/*
|
||||
COLOR_RED 红
|
||||
COLOR_BLACK 黑
|
||||
COLOR_GREEN 绿
|
||||
COLOR_BLUE 蓝
|
||||
COLOR_YELLOW 黄
|
||||
COLOR_WHITE 白
|
||||
COLOR_CYAN 青
|
||||
COLOR_MAGENTA 洋红
|
||||
*/
|
||||
//设置前景颜色
|
||||
void set_fg_color(int color)
|
||||
{// ESC[#m
|
||||
printf("\033[%dm",color);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
//设置背景颜色
|
||||
void set_bg_color(int color)
|
||||
{// ESC[#m
|
||||
printf("\033[%dm",(color+10));
|
||||
fflush(stdout);
|
||||
}
|
34
光标操作/cursor_demo/console.h
Normal file
34
光标操作/cursor_demo/console.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef _CONSOLE_H_
|
||||
#define _CONSOLE_H_
|
||||
|
||||
#define COLOR_RED 31
|
||||
#define COLOR_BLACK 30
|
||||
#define COLOR_GREEN 32
|
||||
#define COLOR_BLUE 34
|
||||
#define COLOR_YELLOW 33
|
||||
#define COLOR_WHITE 37
|
||||
#define COLOR_CYAN 36
|
||||
#define COLOR_MAGENTA 35
|
||||
/*
|
||||
COLOR_RED 红
|
||||
COLOR_BLACK 黑
|
||||
COLOR_GREEN 绿
|
||||
COLOR_BLUE 蓝
|
||||
COLOR_YELLOW 黄
|
||||
COLOR_WHITE 白
|
||||
COLOR_CYAN 青
|
||||
COLOR_MAGENTA 洋红
|
||||
*/
|
||||
|
||||
extern void cusor_moveto(int x, int y);//光标跳转到 y行 x列
|
||||
extern void cusor_get_pos(void);//保存光标位置
|
||||
extern void cusor_hide(void);//隐藏光标
|
||||
extern void cusor_show(void);//显示光标
|
||||
extern void cusor_set_pos(void);//恢复光标位置
|
||||
extern void clear_screen(void);//清屏
|
||||
extern void set_fg_color(int color);//设置字体前景色
|
||||
extern void set_bg_color(int color);//设置字体背景色
|
||||
|
||||
#endif //_CONSOLE_H_
|
||||
|
||||
|
BIN
光标操作/cursor_demo/test
Normal file
BIN
光标操作/cursor_demo/test
Normal file
Binary file not shown.
14
光标操作/cursor_demo/test.c
Normal file
14
光标操作/cursor_demo/test.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include<stdio.h>
|
||||
#include"console.h"
|
||||
int main()
|
||||
{
|
||||
clear_screen();//清屏幕内容
|
||||
cusor_moveto(20, 4);//光标移到 第4行,第20列
|
||||
set_fg_color(COLOR_RED);//设置字体颜色为红色
|
||||
printf("hello ,i love China!!!\n");
|
||||
|
||||
cusor_moveto(20, 2);//光标移到 第2行,第20列
|
||||
set_fg_color(COLOR_BLUE);//设置字体颜色为蓝色
|
||||
printf("hello ,i love sunplusapp!!\n");
|
||||
return 0;
|
||||
}
|
32
常用函数/ColbenFuns.c
Normal file
32
常用函数/ColbenFuns.c
Normal file
@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void gettoks(char *src,TOKS *toks,char *cut)
|
||||
{
|
||||
toks->count=0;
|
||||
toks->tok=(char **)calloc(++(toks->count),sizeof(char *));
|
||||
*(toks->tok)=strtok(src,cut);
|
||||
while(NULL!=*(toks->tok+toks->count-1))
|
||||
{
|
||||
toks->tok=realloc(toks->tok,sizeof(char *)*(++(toks->count)));
|
||||
*(toks->tok+toks->count-1)=strtok(NULL,cut);
|
||||
}
|
||||
(toks->count)--;
|
||||
}
|
||||
|
||||
char getch( )
|
||||
{
|
||||
struct termios oldt, newt;
|
||||
char ch;
|
||||
tcgetattr( STDIN_FILENO, &oldt );
|
||||
newt = oldt;
|
||||
newt.c_lflag &= ~( ICANON | ECHO );
|
||||
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
|
||||
ch = getchar();
|
||||
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
|
||||
return ch;
|
||||
}
|
||||
|
234
常用函数/gtk_functions.c
Normal file
234
常用函数/gtk_functions.c
Normal file
@ -0,0 +1,234 @@
|
||||
//`pkg-config --cflags --libs gtk+-2.0` //编译标志
|
||||
#include <gtk/gtk.h> //头文件
|
||||
int main(int argc,char *argv[]) //主函数
|
||||
{
|
||||
gtk_init(&argc,&argv[]);
|
||||
GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL); //有边框
|
||||
GTK_WINDOW_POPUP //无边框
|
||||
//控件操作
|
||||
gtk_widget_show(window);
|
||||
gtk_main();
|
||||
return 0;
|
||||
}
|
||||
void gtk_window_set_title(GtkWindow *window,const gchar *title); //窗口标题
|
||||
void gtk_widget_set_size_request(GtkWidget *widget,gint width,gint height); //窗口最小大小
|
||||
void gtk_window_set_resizable(GtkWindow *window,gboolean resizable); //窗口伸缩
|
||||
void gtk_window_set_position(GtkWindow *window,GtkWindowPosition position); //窗口位置
|
||||
GTK_WIN_POS_NONE
|
||||
GTK_WIN_POS_CENTER
|
||||
GTK_WIN_POS_MOUSE
|
||||
GTK_WIN_POS_CENTER_ALWAYS
|
||||
GdkPixbuf *gdk_pixbuf_new_from_file(const gchar *filename,GError **error); //创建图片资源
|
||||
NULL
|
||||
GdkPixbuf *gdk_pixbuf_scale_simple(const GdkPixbuf *src,int dest_width,int dest_height,GdkInterpType interp_type); //设置图片大小
|
||||
GDK_INTERP_BILINEAR
|
||||
void g_object_unref(GtkObject *object); //释放资源
|
||||
GtkWidget *gtk_image_new_from_pixbuf(GdkPixbuf *pixbuf); //通过资源创建图片控件
|
||||
void gtk_image_set_from_pixbuf(GtkImage *image,GdkPixbuf *pixbuf); //设置图片控件图片
|
||||
void gtk_image_clear(GtkImage *image); //清除图像控件里的图像数据
|
||||
GtkWidget *gtk_button_new_with_lable(const gcahr *label); //创建带内容按钮
|
||||
void gtk_button_set_label(GtkButton *button,const gchar *label); //设置按钮标签
|
||||
const gchar *gtk_button_get_label(GtkButton *button); //获取按钮标签
|
||||
void gtk_widget_set_sensitive(GtkWidget *widget,gboolean sensitive); //按钮使能
|
||||
void gtk_button_set_image(GtkButton *button,GtkWidget *image); //设置按钮图片
|
||||
void gtk_button_set_relief(GtkButton *button,GtkReliefStyle newstyle); //按钮透明度
|
||||
GTK_RELIEF_NONE //按钮透明
|
||||
void gtk_container_add(GtkContainer *container,GtkWidget *widget); //容器附加控件
|
||||
gulong g_signal_connect(gpointer instance,const gchar *detailed_signal,GCallback c_handler,gpointer data); //信号连接回调
|
||||
GtkWidget *gtk_hbox_new(gboolean homogeneous,gint spacing); //水平布局
|
||||
GtkWidget *gtk_vbox_new(gboolean homogeneous,gint spacing); //垂直布局
|
||||
GtkWidget *gtk_label_new(cost gchar *str); //创建标签
|
||||
void gtk_label_set_text(GtkLabel *label,const gchar *str); //设置标签内容
|
||||
const gchar *gtk_label_get_label(GtkLabel *label); //获取标签内容
|
||||
GtkWidget *gtk_table_new(guint rows,guint columns,gboolean homogeneous); //表格布局
|
||||
void gtk_table_attach_defaults(GtkTable *table,GtkWidget *widget,guint left_attach,guint right_attach,guint top_attach,guint bottom_attach); //表格布局附加控件
|
||||
GtkWidget *gtk_fixed_new(); //固定布局
|
||||
void gtk_fixed_put(GtkFixed *fixed,GtkWidget *widget,gint x,gint y); //固定布局附加控件
|
||||
GtkWidget *gtk_entry_new(void); //行编辑
|
||||
gtk_entry_set_visibility(GtkEntry *entry,gboolean visible); //行编辑显示
|
||||
const gchar *gtk_entry_get_text(GtkEngry *entry); //获取行编辑内容
|
||||
void gtk_entry_set_text(GtkEngry *entry,const gchar *text); //设置行编辑内容
|
||||
"activate" //行编辑触发信号
|
||||
GtkWidget *gtk_process_bar_new(); //进度条
|
||||
void gtk_process_bar_set_fraction(GtkProgressBar *pbar,gdouble fraction); //设置进度条百分比
|
||||
void gtk_process_bar_set_text(GtkProgressBar *pbar,gchar *text); //设置滑槽文本显示
|
||||
void gtk_progress_bar_set_orientation(GtkProgressBar *pbar,GtkProgressBarOrientation orientation); //设置进度条移动方向
|
||||
GTK_PROGRESS_LEFT_TO_RIGHT
|
||||
GTK_PROGRESS_RIGHT_TO_LEFT
|
||||
GTK_PROGRESS_BOTTOM_TO_TOP
|
||||
GTK_PROGRESS_TOP_TO_BOTTOM
|
||||
GtkWidget *gtk_scrolled_window_new(GtkAdjustment *hadjustment,GtkAdjustment *vadjustment); //滚动窗口
|
||||
NULL NULL
|
||||
void gtk_scolled_window_set_policy(GtkScolledWindow *scrolled_window,GtkPolicyTypehscrollbar_policy,GtkPolicyType vscrollbar_policy); //滚动条出现的方式
|
||||
GTK_POLICY_AUTOMATIC
|
||||
GTK_POLICY_ALWAYS
|
||||
GTK_POLICY_NEVER
|
||||
GtkWidget *gtk_clist_new_with_titles(gint columns,gchar *titles[]); //创建列表
|
||||
void gtk_clist_set_column_width(GtkCList *clist,gint column,gint width); //设置列宽
|
||||
void gtk_clist_set_column_justification(GtkCList *clist,gint column,GtkJustification justification); //对齐方式
|
||||
GTK_JUSTIFY_LEFT
|
||||
GTK_JUSTIFY_RIGHT
|
||||
GTK_JUSTIFY_CENTER
|
||||
GTK_JUSTIFY_CENTER
|
||||
gint gtk_clist_append(GtkCList *clist,gchar *text[]); //列表添加行
|
||||
void gtk_clist_clear(GtkCList *clist); //清空列表所有行
|
||||
gint gtk_clist_get_text(GtkCList *clist,gint row,gint column,gchar **text); //获取某行某列内容
|
||||
"select_row" //列表行出发信号
|
||||
guint g_timeout_add(guint interval,GSourceFunc function,gpointer data); //创建定时器
|
||||
gboolean g_source_remove(guint tag); //移除定时器
|
||||
void gtk_widget_add_events(GtkWidget *widget,gint events); //控件捕获事件
|
||||
GDK_BUTTON_PRESS_MASK
|
||||
GDK_BUTTON_RELEASE_MASK
|
||||
GDK_BUTTON_MOTION_MASK
|
||||
GDK_KEY_PRESS_MASK
|
||||
GDK_ENTER_NOTIFY_MASK
|
||||
"button_press_event" //鼠标点击事件信号
|
||||
"button_release_event" //鼠标释放事件信号
|
||||
gboolean callback(GtkWidget *widget,GdkEventButton *event,gpointer data); //鼠标点击事件回调
|
||||
event->x,event->y //获取点击坐标值
|
||||
event->button //1左键,2中键,3右键
|
||||
event->type //双击GDK_2BUTTON_PRESS
|
||||
return TRUE;
|
||||
"motion_notify_event" //鼠标移动事件信号
|
||||
gboolean callback(GtkWidget *widget,GdkEventMotion *event,gpointer data); //鼠标移动事件回调
|
||||
event->x,event->y //获取移动坐标值
|
||||
return TRUE;
|
||||
"key_press_event" //键盘按下事件的信号
|
||||
"key_release_event" //键盘释放事件的信号
|
||||
gboolean callback(GtkWidget *widget,GdkEventKey *event,gpointer data); //键盘按下事件回调
|
||||
event->keyval //获取键值
|
||||
return TRUE;
|
||||
"expose_event" //曝光事件信号
|
||||
gboolean callback(GtkWidget *widget,GdkEventExpose *event,gpointer data); //曝光事件回调
|
||||
return FALSE;
|
||||
#include <cairo.h> //Cairo绘图头文件
|
||||
void gtk_widget_set_app_paintable(GtkWidgte *widget,gboolean app_paintable); //允许窗口绘图
|
||||
cairo_t *gtk_cairo_create(GdkDrawable *drawable); //创建Cairo环境
|
||||
drawable->window
|
||||
void cairo_destroy(cairo_t *cr); //回收资源
|
||||
void cairo_set_line_width(cairo_t *cr,double width); //设置线宽
|
||||
void cairo_set_source_rgb(cairo_t *cr,double r,double g,double b); //设置颜色,r,g,b从0.0到1.0
|
||||
void cairo_move_to(cairo_t *cr,double x,double y); //设置直线起点
|
||||
void cairo_line_to(cairo_t *cr,double x,double y); //设置直线终点
|
||||
void cairo_rectangle(cairo_t *cr,double x,double y,double width,double height); //设置矩形路径
|
||||
void cairo_stroke(cairo_t *cr); //绘制cairo
|
||||
void cairo_fill(cairo_t *cr); //填充cairo闭合路径
|
||||
void cairo_select_font_face(cairo_t *cr,const char *family,cairo_font_slant_t slant,cairo_font_weight_t weight);
|
||||
字体 倾斜 加粗
|
||||
void cairo_set_font_size(cairo_t *cr,double size); //设置字体大小
|
||||
void cairo_show_text(cairo_t *cr,const char *utf8); //显示字符串
|
||||
gdk_cairo_set_source_pixbuf(cairo_t *cr,const GdkPixbuf *pixbuf,double pixbuf_x,double pixbuf_y); //设置画图图片
|
||||
void cairo_paint(cairo_t *cr); //绘制图片
|
||||
void gtk_widget_queue_draw(GtkWidget *widget); //刷新绘图区域
|
||||
void gtk_thread_init(void) //gtk创建线程前调用/*{{{*/
|
||||
{
|
||||
if(FALSE == g_thread_supported())
|
||||
{
|
||||
g_thread_init(NULL);
|
||||
}
|
||||
gdk_threads_init();
|
||||
}/*}}}*/
|
||||
void *pthread_fun(void *data) //线程函数中操作界面需要封装/*{{{*/
|
||||
{
|
||||
gdk_threads_enter();
|
||||
//一个或多个界面操作函数
|
||||
gdk_threads_leave();
|
||||
}/*}}}*/
|
||||
/* 功能: 设置背景图
|
||||
* widget: 主窗口
|
||||
* w, h: 图片的大小
|
||||
* path: 图片路径
|
||||
*/
|
||||
static void chang_background(GtkWidget *widget, int w, int h, const gchar *path)/*{{{*/
|
||||
{
|
||||
gtk_widget_set_app_paintable(widget, TRUE); //允许窗口可以绘图
|
||||
gtk_widget_realize(widget);
|
||||
|
||||
/* 更改背景图时,图片会重叠
|
||||
* 这时要手动调用下面的函数,让窗口绘图区域失效,产生窗口重绘制事件(即 expose 事件)。
|
||||
*/
|
||||
gtk_widget_queue_draw(widget);
|
||||
|
||||
GdkPixbuf *src_pixbuf = gdk_pixbuf_new_from_file(path, NULL); // 创建图片资源对象
|
||||
// w, h是指定图片的宽度和高度
|
||||
GdkPixbuf *dst_pixbuf = gdk_pixbuf_scale_simple(src_pixbuf, w, h, GDK_INTERP_BILINEAR);
|
||||
|
||||
GdkPixmap *pixmap = NULL;
|
||||
|
||||
/* 创建pixmap图像;
|
||||
* NULL:不需要蒙版;
|
||||
* 123: 0~255,透明到不透明
|
||||
*/
|
||||
gdk_pixbuf_render_pixmap_and_mask(dst_pixbuf, &pixmap, NULL, 128);
|
||||
// 通过pixmap给widget设置一张背景图,最后一个参数必须为: FASLE
|
||||
gdk_window_set_back_pixmap(widget->window, pixmap, FALSE);
|
||||
|
||||
// 释放资源
|
||||
g_object_unref(src_pixbuf);
|
||||
g_object_unref(dst_pixbuf);
|
||||
g_object_unref(pixmap);
|
||||
}/*}}}*/
|
||||
|
||||
/* 功能: 设置控件字体大小
|
||||
* widget: 需要改变字体的控件
|
||||
* size: 字体大小
|
||||
* is_button: TRUE代表控件为按钮,FALSE为其它控件
|
||||
*/
|
||||
static void set_widget_font_size(GtkWidget *widget, int size, gboolean is_button)/*{{{*/
|
||||
{
|
||||
GtkWidget *labelChild;
|
||||
PangoFontDescription *font;
|
||||
gint fontSize = size;
|
||||
|
||||
font = pango_font_description_from_string("Sans"); //"Sans"字体名
|
||||
pango_font_description_set_size(font, fontSize*PANGO_SCALE);//设置字体大小
|
||||
|
||||
if(is_button){
|
||||
labelChild = gtk_bin_get_child(GTK_BIN(widget));//取出GtkButton里的label
|
||||
}else{
|
||||
labelChild = widget;
|
||||
}
|
||||
|
||||
//设置label的字体,这样这个GtkButton上面显示的字体就变了
|
||||
gtk_widget_modify_font(GTK_WIDGET(labelChild), font);
|
||||
pango_font_description_free(font);
|
||||
}/*}}}*/
|
||||
|
||||
// 给创建好的image重新设计一张图片
|
||||
void load_image(GtkWidget *image, const char *file_path, const int w, const int h )/*{{{*/
|
||||
{
|
||||
gtk_image_clear( GTK_IMAGE(image) ); // 清除图像
|
||||
GdkPixbuf *src_pixbuf = gdk_pixbuf_new_from_file(file_path, NULL); // 创建图片资源
|
||||
GdkPixbuf *dest_pixbuf = gdk_pixbuf_scale_simple(src_pixbuf, w, h, GDK_INTERP_BILINEAR); // 指定大小
|
||||
gtk_image_set_from_pixbuf(GTK_IMAGE(image), dest_pixbuf); // 图片控件重新设置一张图片(pixbuf)
|
||||
g_object_unref(src_pixbuf); // 释放资源
|
||||
g_object_unref(dest_pixbuf); // 释放资源
|
||||
}/*}}}*/
|
||||
|
||||
/***********************************************
|
||||
* 函数功能:设置窗口显示字体颜色
|
||||
* 参数类型:widget:需要改变颜色的控件
|
||||
color_buf:设置字体颜色,如"red"、"blue"
|
||||
is_button:是否为button
|
||||
* 返回类型:成功返回0,失败返回-1
|
||||
* 维护记录:2013-12-18 by lihuibo
|
||||
***********************************************/
|
||||
int sungtk_widget_set_font_color(GtkWidget *widget, const char *color_buf, gboolean is_button)/*{{{*/
|
||||
{
|
||||
if(widget == NULL && color_buf==NULL)
|
||||
return -1;
|
||||
|
||||
GdkColor color;
|
||||
GtkWidget *labelChild = NULL;
|
||||
sungtk_color_get(color_buf, &color);
|
||||
if(is_button == TRUE){
|
||||
labelChild = gtk_bin_get_child(GTK_BIN(widget));//取出GtkButton里的label
|
||||
gtk_widget_modify_fg(labelChild, GTK_STATE_NORMAL, &color);
|
||||
gtk_widget_modify_fg(labelChild, GTK_STATE_SELECTED, &color);
|
||||
gtk_widget_modify_fg(labelChild, GTK_STATE_PRELIGHT, &color);
|
||||
}else{
|
||||
gtk_widget_modify_fg(widget, GTK_STATE_NORMAL, &color);
|
||||
}
|
||||
return 0;
|
||||
}/*}}}*/
|
||||
|
114
常用函数/netword_functions.c
Normal file
114
常用函数/netword_functions.c
Normal file
@ -0,0 +1,114 @@
|
||||
#include <arpa/inet.h> //字节序、地址转换头文件
|
||||
uint32_t htonl(uint32_t hostint32); //32位主机字节序转换成网络字节序,成功返回网络字节序的值
|
||||
uint16_t htons(uint16_t hostint16); //16位主机字节序转换成网络字节序,成功返回网络字节序的值
|
||||
uint32_t ntohl(uint32_t netint32); //32位网络字节序转换成主机字节序,成功返回主机字节序的值
|
||||
uint16_t ntohs(uint16_t netint16); //16位网络自己序转换成主机字节序,成功返回主机字节序的值
|
||||
int inet_pton(int family,const char *strptr,void *addrptr); //点分十进制数串转换成32位无符号整数,成功返回1
|
||||
const char *inet_ntop(int family,const void *addrptr,char *strptr,size_t len); //32位无符号整数转换成点分十进制数串,成功返回首地址,失败NULL
|
||||
INET_ADDRSTRLEN 16
|
||||
INET6_ADDRSTRLEN 16
|
||||
#include <sys/socket.h> //套接字头文件
|
||||
int socket(int family,int type, int protocol); //创建套接字,成功返套接字
|
||||
AF_INET SOCK_DGRAM 0
|
||||
AF_INET6 SOCK_STREAM IPPROTO_TCP
|
||||
PF_PACKET SOCK_RAW IPPROTO_UDP
|
||||
#include <netinet/in.h> //地址结构头文件
|
||||
struct in_addr
|
||||
{
|
||||
in_addr_t s_addr; //4 byte
|
||||
};
|
||||
struct sockaddr_in //ipv4套接字地址结构,定义时使用
|
||||
{
|
||||
sa_family_t sin_family; //2 byte
|
||||
in_port_t sin_port; //2byte
|
||||
struct in_addr sin_addr; //4 byte
|
||||
char sin_zero[8]; //8 byte
|
||||
};
|
||||
struct sockaddr //通用套接字地址结,接口函数调用时使用
|
||||
{
|
||||
sa_family_t sa_family; //2 byte
|
||||
char sa_data[14]; //14 byte
|
||||
};
|
||||
//INADDR_ANY 本地通配地址,值为0
|
||||
ssize_t sendto(int sockfd,const void *buf,size_t nbytes,int flags,const struct sockaddr *to,socklen_t addrlen); //发送UDP数据至to指定的ip,成功返回发送字节数,失败返回-1
|
||||
0
|
||||
int bind(int sockfd,const struct sockaddr *myaddr,socklen_t addrlen); //绑定本地协议地址和sockfd,成功返回0
|
||||
ssize_t recvfrom(int sockfd,void *buf,size_t nbytes,int flags,struct sockaddr *from,socklen_t *addrlen); //接收UDP数据,保存至from指向的结构,成功返回接收到的字符数,失败-1
|
||||
0
|
||||
//TFTP:简单文件传送协议,基于UDP,无用户有效认证,数据传输模式有二进制模式octet和文本模式netascii
|
||||
//69号端口等待客户端请求,临时端口与客户端通信,每个数据包变化从1开始,需客户端ACK确认,超时重发
|
||||
//TCP客户端:知道“服务器”的ip、port,主动连接
|
||||
#include <sys/socket.h> //TCP客户端头文件
|
||||
int connect(int sockfd,const struct sockaddr *addr,socklen_t len); //主动连接服务器,成功返回0
|
||||
ssize_t send(int sockfd,const void *buf,size_t nbytes,int flags); //发送数据,返回成功发送的字节数,不能发送0长度的数据包
|
||||
ssize_t recv(int sockfd,void *buf,size_t nbytes,int flags); //接受网络数据,返回成功接收的字节数
|
||||
//TCP服务器具备确定地址,明确是服务器,等待连接
|
||||
int listen(int sockfd,int backlog); //套接字主动变被动,成功返回0
|
||||
int accept(int sockfd,struct sockaddr *cliaddr,socklen_t *addrlen); //从已连接队列中取出一个,否则阻塞,返回已连接套接字
|
||||
//关闭一个已连接套接字将导致另一端接收到一个0长度的数据包
|
||||
//----------------------------------------------------------------------------
|
||||
//多线程TCP服务器并发
|
||||
#include <stdio.h>/*{{{*/
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>/*}}}*/
|
||||
|
||||
void *client_fun(void *arg) //与客户端交互线程
|
||||
{
|
||||
int connect_fd=(int)arg; //客户端套接字
|
||||
|
||||
close(connect_fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc,char *argv[])
|
||||
{
|
||||
int sock_fd=socket(AF_INET,SOCK_STREAM,0); //创建sockfd/*{{{*/
|
||||
if(0 > sock_fd)
|
||||
{
|
||||
perror("\nsocket>>>\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct sockaddr_in own_addr,client_addr; //服务器自身和客户端地址结构
|
||||
bzero(&own_addr,sizeof(own_addr));
|
||||
own_addr.sin_family=AF_INET;
|
||||
own_addr.sin_port=htons(8000);
|
||||
own_addr.sin_addr.s_addr=htonl(INADDR_ANY);
|
||||
|
||||
socklen_t own_addr_len=sizeof(own_addr); //地址结构长度
|
||||
socklen_t client_addr_len=sizeof(client_addr);
|
||||
|
||||
if(0 != bind(sock_fd,(struct sockaddr *)&own_addr,own_addr_len)) //绑定
|
||||
{
|
||||
perror("\nbind>>>\n");
|
||||
close(sock_fd);
|
||||
return -2;
|
||||
}
|
||||
|
||||
if(0 != listen(sock_fd,10)) //监听
|
||||
{
|
||||
perror("\nlisten>>>\n");
|
||||
close(sock_fd);
|
||||
return -3;
|
||||
}/*}}}*/
|
||||
|
||||
while(1)
|
||||
{
|
||||
bzero(&client_addr,sizeof(client_addr));
|
||||
int connect_fd=accept(sock_fd,(struct sockaddr *)&client_addr,&client_addr_len);
|
||||
pthread_t tid;
|
||||
pthread_create(&tid,NULL,(void *)client_fun,(void *)connect_fd);
|
||||
pthread_detach(tid);
|
||||
}
|
||||
|
||||
close(sock_fd); //关闭监听套接字
|
||||
return 0;
|
||||
}
|
||||
//--------------------------------------------------------------------------------
|
||||
|
BIN
常用函数/posix定义的取消点1.bmp
Normal file
BIN
常用函数/posix定义的取消点1.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 746 KiB |
BIN
常用函数/posix定义的取消点2.bmp
Normal file
BIN
常用函数/posix定义的取消点2.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.2 MiB |
67
常用函数/sql_functions.c
Normal file
67
常用函数/sql_functions.c
Normal file
@ -0,0 +1,67 @@
|
||||
//integer:带符号整形(最多64位)
|
||||
//real:8字节浮点
|
||||
//text:字符,无大小限制
|
||||
//bolb:任意类型,无大小限制
|
||||
//null:空值
|
||||
//! sqlite3 *.db 创建或打开*.db文件
|
||||
//! .quit或.exit 退出数据库
|
||||
//create table 表名称 (列名称 数据类型,……); 创建表
|
||||
//primary key 主键,每个表只能有一个,其列数据不可重复
|
||||
//.tables 查看表
|
||||
//.schema 查看表结构
|
||||
//alter table 表名 add 列名 数据类型; 添加新列
|
||||
//alter table 表名 rename to 新表名; 修改表名
|
||||
//drop table 表名称; 删除表
|
||||
//insert into 表名 values (列值……); 插入新行并赋值,字符串加‘’号
|
||||
//insert into 表名 (列名……) values (列值……); 插入新行部分赋值
|
||||
//update 表名 set 列……=值…… 匹配条件; 修改where匹配行的列值,空匹配所有
|
||||
//delete from 表名 匹配条件; 删除匹配行
|
||||
//select * from 表名 匹配条件;
|
||||
//select 列名…… from 表名 匹配条件; 从表中选取数据
|
||||
//.mode colum 左对齐列
|
||||
//.headers on 列名显示
|
||||
//where 列名 in (列值……) 在where子句中规定多个值
|
||||
//where 列1=值1 and 列2=值2 and ... 多个条件与
|
||||
//where 列1=值1 or 列2=值2 or ... 多个条件或
|
||||
//where 列名 between A and B 选取A、B之间的数据范围
|
||||
//where 列名 like 列值 数字相当于“=”,字符串可用“%”通配
|
||||
//where 列名 not in/between/like ...
|
||||
//where not 列1=值1 and/or 列2=值2 and/or ... 原结果集补集
|
||||
//order by 列名 (desc) 根据指定列对结果集排序,用dest降序
|
||||
//begin; 开始一个事务
|
||||
//commit; 确认begin后的全部命令
|
||||
//rollback; 取消begin后的所有操作
|
||||
int sqlite3_open(char *db_name,sqlite3 **db); //打开数据库,成功返回SQLITE_OK
|
||||
int sqlite3_close(sqlite3 *db); //关闭数据库,成功返回SQLITE_OK,链接时添加 -lpthread 和 -ldl两个库
|
||||
int sqlite3_exec(sqlite3 *db,const char *sql,exechandler_t callback,void *arg,char **errmsg); //执行sql语句,若结果集不空,回调下面callback
|
||||
typedef int (*exechandler_t)(void *para,int n_column,char **column_value,char **column_name);
|
||||
int sqlite3_get_table(sqlite3 *db,const char *sql,char ***resultp,int *nrow,int *ncolumn,char **errmsg); //执行sql语句,结果集保存在resultp中
|
||||
void sqlite3_free_table(char **resultp); //释放sqlite3_get_table分配的内存
|
||||
//函数
|
||||
//length() 返回字符串长度
|
||||
//lower() 将字符串转换为小写
|
||||
//upper() 将字符串转换为大写
|
||||
//select 函数名(列名) from 表名;
|
||||
//聚集函数
|
||||
//avg() 返回某列的平均值
|
||||
//count() 返回某列的行数
|
||||
//max() 返回某列的最大值
|
||||
//min() 返回某列的最小值
|
||||
//sum() 返回某列值之和
|
||||
//select 列名…… from 表名 group by 列名; 根据某一属性重新分组,出现在where子句之后
|
||||
//select 函数名 (列名) …… from 表名 group by 列名 having 函数名 (列名) 限制值; 查看满足having条件的分组
|
||||
//主键惟一标识一行,对用户无意义,常用语索引,勿更新,无动态变化数据,由计算机自动生成
|
||||
//unique 用法同主键,保证一个列中数据惟一,可以有多个,可以修改和更新
|
||||
//create table 表名 (列名 数据类型 check (判断语句)); 保证某列数据满足一定条件
|
||||
//select 列名…… from 表…… where 判断语句; 用“.”分隔表名和列名
|
||||
//create view 视图名 as 语句; 创建视图
|
||||
//drop view 视图名; 删除视图
|
||||
//create trigger 触发器名 before/after insert/update/delete on 表名
|
||||
//begin 语句; end;
|
||||
//datetime('now') 获取当前系统时间
|
||||
//.tables 查看触发器
|
||||
//drop trigger 触发器名; 删除触发器
|
||||
//create index 索引名 on 表名(列名); //创建索引
|
||||
//.indices 查看索引
|
||||
//drop index 索引名; 删除索引
|
||||
|
205
常用函数/sys_functions.c
Normal file
205
常用函数/sys_functions.c
Normal file
@ -0,0 +1,205 @@
|
||||
//#######################################################################
|
||||
FILE *fopen(const char *path, const char *mode); //打开文件,失败返回NULL
|
||||
//参数说明:
|
||||
//参数path字符串包含欲打开的文件路径及文件名,参数mode字符串则代表着流形态。
|
||||
//mode有下列几种形态字符串:
|
||||
//r 以只读方式打开文件,该文件必须存在。
|
||||
//r+ 以可读写方式打开文件,该文件必须存在。
|
||||
//rb+ 读写打开一个二进制文件,允许读写数据,文件必须存在。
|
||||
//rw+ 读写打开一个文本文件,允许读和写。
|
||||
//w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。
|
||||
//w+ 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。
|
||||
//a 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。(EOF符保留)
|
||||
//a+ 以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 (原来的EOF符不保留)
|
||||
//wb 只写打开或新建一个二进制文件;只允许写数据。
|
||||
//wb+ 读写打开或建立一个二进制文件,允许读和写。
|
||||
//ab+ 读写打开一个二进制文件,允许读或在文件末追加数据。
|
||||
//at+ 打开一个叫string的文件,a表示append,就是说写入处理的时候是接着原来文件已有内容写入,不是从头写入覆盖掉,t表示打开文件的类型是文本文件,+号表示对文件既可以读也可以写。
|
||||
//上述的形态字符串都可以再加一个b字符,如rb、w+b或ab+等组合,加入b 字符用来告诉函数库以二进制模式打开文件。如果不加b,表示默认加了t,即rt,wt,其中t表示以文本模式打开文件。由fopen()所建立的新文件会具有S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH(0666)权限,此文件权限也会参考umask值。
|
||||
//有些C编译系统可能不完全提供所有这些功能,有的C版本不用"r+","w+","a+",而用"rw","wr","ar"等,读者注意所用系统的规定。
|
||||
int fclose(FILE *stream); //关闭一个文件流,成功返回0, 失败返回EOF
|
||||
int feof(FILE *stream); //文件结束返回非0
|
||||
int fseek(FILE *stream, long offset, int fromwhere); //文件位置指针偏移,成功返回0
|
||||
//0文件头,1当前,2文件尾
|
||||
long ftell(FILE *stream); //返回文件位置指针当前位置
|
||||
int fgetc(FILE *stream); //结尾或出错返回EOF
|
||||
int fputc(int n, FILE *fp); //失败返回EOF
|
||||
char *fgets(char *buf, int bufsize, FILE *stream); //出错返回NULL
|
||||
int fputs(const char *str, FILE *fp); //向文件写入字符串,失败返回EOF
|
||||
size_t fread(void *buffer, size_t size, size_t count, FILE *stream); //读取文件中count*size个字节,成功返回读取到的元素个数(count),否则返回0
|
||||
size_t fwrite(const void *buffer, size_t size, size_t count, FILE *stream); //返回实际写入的数据块数目
|
||||
int fscanf(FILE *stream, const char *format, [argument...]);
|
||||
int fprintf(FILE *stream, const char *format, [argument...]);
|
||||
//#######################################################################
|
||||
#define STDIN_FILENO 0 //标准输入
|
||||
#define STDOUT_FILENO 1 //标准输出
|
||||
#define STDERR_FILENO 2 //标准错误
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h> //open、close头文件
|
||||
int open(const char *pathname,int flags); //文件存在
|
||||
int open(const char *pathname,int flags,mode_t mode); //文件不存在
|
||||
//成功返回fd,失败返回-1
|
||||
int close (int fd); //关闭文件,成功返回0,失败返回-1
|
||||
#include <unistd.h> //write,read,remove头文件
|
||||
ssize_t write(int fd,const void *addr,size_t count); //写文件,成功返回写入字节个数,失败返回-1
|
||||
ssize_t read(int fd,void *addr,size_t count); //读文件,成功返回读出字节个数,失败返回-1
|
||||
int remove(const char *pathname); //删除文件,成功返回0,失败返回-1
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h> //进程函数头文件
|
||||
pid_t getpid(); //获取本进程号
|
||||
pid_t getppid(); //获取父进程号
|
||||
pid_t getpgid(); //获取进程组号
|
||||
pid_t fork(); //在已有进程中创建子进程
|
||||
//成功,子进程返回0,父进程返回子进程ID;失败,返回-1
|
||||
unsigned int sleep(unsigned int sec); //进程挂起指定秒数
|
||||
//成功返回0,中断返回剩余秒数
|
||||
#include <sys/wait.h> //wait、waitpid头文件
|
||||
pid_t wait(int *status); //等待子进程结束回收其资源
|
||||
//成功返回子进程号,失败返回-1
|
||||
WIFEXITED(status); //子进程正常终止取出字段值非零
|
||||
WEXITSTATUS(status) //返回保存在status变量8~16位的子进程退出状态(正常退出才可使用)
|
||||
pid_t waitpid(pid_t pid,int *status,int options); //等待子进程结束回收其资源
|
||||
//成功返回子进程号,失败返回-1
|
||||
#include <stdlib.h> //exit头文件
|
||||
void exit(int value); //结束进程执行
|
||||
#include <unistd.h> //_exit头文件,value低八位有效
|
||||
void _exit(int value); //结束进程执行,value低八位有效
|
||||
#include <stdlib.h> //atexit头文件
|
||||
int atexit(void (*function)(void)); //注册进程结束前调用的函数
|
||||
pid_t vfork(); //创建子进程,出错返回-1
|
||||
#include <unistd.h> //exec函数组头文件
|
||||
int execl(const char *pathname,const char *arg0,...,NULL);
|
||||
int execlp(const char *filename,const char *arg0,...,NULL);
|
||||
int execle(const char *pathname,const char *arg0,...,NULL,char *const envp[]);
|
||||
int execv(const char *pathname,char *const argv[]);
|
||||
int execvp(const char *filename,char *const argv[]);
|
||||
int execve(const char *pathname,char *const argv[],char *const envp[]);
|
||||
//exec函数组成功无返回,失败返回-1
|
||||
#include <stdlib.h> //system头文件
|
||||
int system(const char *command); //调用系统命令,command空时返回非0,失败返回127或-1
|
||||
//Ctrl+c产生中断信号:SIGINT
|
||||
//Ctrl+\产生中断信号:SIGQUIT
|
||||
//Ctrl+z产生中断信号:SIGSTOP
|
||||
//SIGKILL和SIGSTOP的处理方式不可更改
|
||||
#include <sys/types.h>
|
||||
#include <signal.h> //kill头文件
|
||||
int kill(pid_t pid,int signum); //给进程发送信号,成功返回0,失败返回-1
|
||||
#include <unistd.h> //alarm头文件
|
||||
unsigned int alarm(unsigned int seconds); //seconds秒后发出SIGALRM信号,默认终止
|
||||
#include <signal.h> //raise头文件
|
||||
int raise(int signum); //给进程发送信号,成功返回0,失败返回-1
|
||||
#include <stdlib.h> //abort头文件
|
||||
void abort(); //默认向进程发送SIGABRT信号,默认退出
|
||||
//终止前刷新缓冲区,关闭文件描述符
|
||||
#include <unistd.h> //pause头文件
|
||||
int pause(); //挂起进程直至捕捉到信号,成功返回-1
|
||||
#include <signal.h> //signal头文件
|
||||
typedef void (*sighandler_t)(int);
|
||||
sighandler_t signal(int signum,sighandler_t handler); //注册信号处理函数
|
||||
SIG_IGN //忽略信号
|
||||
SIG_DFL //执行默认动作
|
||||
自定义
|
||||
#include <signal.h> //信号集头文件
|
||||
int sigemptyset(sigset_t *set); //初始化一个空信号集,成功返回0,失败返回-1
|
||||
int sigfillset(sigset_t *set); //初始化为所有信号的集合,成功返回0,失败返回-1
|
||||
int sigismember(const sigset_t *set,int signum); //查询signum是否在set中,在返回1,不在返回0,错误返回-1
|
||||
int sigaddset(sigset_t *set,int signum); //加signum到set中,成功返回0,失败返回-1
|
||||
int sigdelset(sigset_t *set,int signum); //从set中删除signum,成功返回0,失败返回-1
|
||||
int sigprocmask(int how,const sigset_t *set,sigset_t *oldset); //编辑阻塞集,成功返回0,失败返回-1
|
||||
SIG_BLOCK //添加set集合
|
||||
SIG_UNBLOCK //删除set集合
|
||||
SIG_SETMASK //设置为set集合
|
||||
#include <unistd.h> //管道头文件
|
||||
int pipe(int fd[2]); //创建pipe,成功返回0,失败返回-1
|
||||
void fcntl(fd,F_SETFL,0); //设置文件阻塞
|
||||
O_NONBLOCK //设置文件非阻塞
|
||||
int dup(int oldfd); //成功返回新文件描述符,失败返回-1
|
||||
int dup2(int oldfd,int newfd); //强制分配并返回newfd,失败返回-1
|
||||
//close_on_exec 标志决定文件描述符在执行exec之后是否可用
|
||||
int flags;
|
||||
flags=fcntl(fd,F_GETFD); //获得标志
|
||||
flags|=FD_CLOEXEC; //打开标志位
|
||||
flags&=~FD_CLOEXEC; //关闭标志位
|
||||
fcntl(fd,F_SETFD,flags); //设置标志
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h> //mkfifo头文件
|
||||
int mkfifo(const char *pathname,mode_t mode); //创建fifo,成功返回0,文件已存在返回-1
|
||||
//不指定NONBLOCK
|
||||
//open只读打开FIFO,阻塞至某进程为写打开FIFO
|
||||
//open只写打开FIFO,阻塞至某进程为读打开FIFO
|
||||
//open以只读只写打开FIFO,调用read阻塞
|
||||
//通信中写退出,read不阻塞,写恢复,read阻塞
|
||||
//通信中读退出,写进程也退出
|
||||
//缓冲区满时write阻塞
|
||||
//open以可读可写打开FIFO,open不阻塞,read阻塞,缓冲区满时write阻塞
|
||||
#include <sys/types.h>
|
||||
#include <sys/ipc.h> //ftok头文件
|
||||
key_t ftok(const char *pathname, int proj_id); //获取IPC键值,成功返回key,失败返回-1
|
||||
#include <sys/msg.h> //msgget头文件
|
||||
int msgget(key_t,int msgflg); //创建消息队列,成功返回消息队列标识符,失败返回-1
|
||||
typedef struct
|
||||
{
|
||||
long mtype;
|
||||
char mtext[100];
|
||||
}MSG; //消息格式
|
||||
int msgsnd(int msgid,const void *msgp,size_t msgsz,int msgflg); //添加消息到队列,成功返回0,失败返回-1
|
||||
ssize_t msgrcv(int msgid,void *msgp,size_t msgsz,long msgtyp,int msgflg); //接收消息,成功返回读取消息的长度,失败返回-1
|
||||
int msgctl(int msgid,int cmd,struct msgid_ds *buf); //控制消息队列属性,成功返回0,失败返回-1
|
||||
IPC_RMID
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/shm.h> //共享内存头文件
|
||||
int shmget(key_t key,size_t size,int shmflg); //创建或打开共享内存,成功返回共享内存标识符,失败返回-1
|
||||
void *shmat(int shmid,const void *shmaddr,int shmflg); //映射,成功返回映射地址,失败返回-1
|
||||
NULL 0
|
||||
int shmdt(const void *shmaddr); //分离共享内存,成功返回0,失败返回-1
|
||||
int shmctl(int shmid,int cmd,struct shmid_ds *buf); //共享内存控制,成功返回0,失败返回-1
|
||||
IPC_RMID //删除
|
||||
void *shmat(int shmid,const void *shmaddr,int shmflg); //映射,成功返回映射地址,失败返回-1
|
||||
#include <pthread.h> //线程头文件,编译加参数 -lpthread
|
||||
int pthread_create(pthread_t *thread,const thread_attr_t *attr,void *(*start_routine)(void *),void *arg); //创建线程
|
||||
NULL
|
||||
int pthread_join(pthread_t thread,void **retval); //等子进程结束回收资源,成功返回0,失败返回非0,有阻塞
|
||||
int pthread_detach(pthread_t thread); //独立线程,终止时系统回收资源,成功返回0,失败返回非0
|
||||
//不终止进程的前提下终止线程
|
||||
//1 线程从执行函数中返回
|
||||
//2 线程调用pthread_exit
|
||||
//3 被其他线程取消
|
||||
void pthread_exit(void *retval); //退出线程
|
||||
int pthread_cancel(pthread_t thread); //发送取消线程信号,成功返回0,失败返回出错编号,无阻塞
|
||||
pthread_setcancelstate(int state,int *old_state); //设置线程是否可被取消,默认是
|
||||
PTHREAD_CANCEL_DISABLE //不可取消
|
||||
PTHREAD_CANCEL_ENABEL //可以取消
|
||||
//出现取消点的函数见posix*.bmp
|
||||
void pthread_setcanceltype(int type,int *oldtype); //立即终止线程
|
||||
PTHREAD_CANCEL_ASYNCHRONOUS //立即取消
|
||||
PTHREAD_CANCEL_DEFERRED //不立即取消
|
||||
void pthread_cleanup_push(void (*routine)(void *),void *arg); //注册线程清理函数
|
||||
void pthread_cleanup_pop(int execute); //删除清理函数,0==execute?不清理:清理
|
||||
//调用清理函数
|
||||
//1 调用pthread_exit退出线程
|
||||
//2 相应其他线程取消要求
|
||||
//3 非零execute调用pthread_cleanup_pop
|
||||
int pthread_mutex_init(pthread_mutex_t *mutex,const pthread_mutexattr_t *arrt); //初始化互斥锁,成功返回0,失败返回非0
|
||||
NULL //默认属性
|
||||
int pthread_mutex_lock(pthread_mutex_t *mutex); //上锁,阻塞至解锁,成功返回0,失败返回非0
|
||||
int pthread_mutex_trylock(pthread_mutex_t *mutex); //上锁,否则立即返回,成功返回0,失败返回非0,非阻塞
|
||||
int pthread_mutex_unlock(pthread_mutex_t *mutex); //解锁,成功返回0,失败非0
|
||||
int pthread_mutex_destroy(pthread_mutex_t *mutex); //销毁锁,成功返回0,失败返回非0
|
||||
#include <semaphore.h> //信号量头文件
|
||||
int sem_init(sem_t *sem,int pshared,unsigned int value); //创建初始化信号量,成功返回0,失败返回-1
|
||||
//pshared==0?线程:进程
|
||||
//信号初始值
|
||||
int sem_wait(sem_t *sem); //信号量值减1,小于0阻塞,成功返回0,失败返回-1
|
||||
int sem_trywait(sem_t *sem); //同上,不阻塞
|
||||
int sem_post(sem_t *sem); //信号值加1等待唤醒,阻塞,成功返回0,失败返回-1
|
||||
int sem_getvalue(sem_t *sem,int *sval); //sval保存sem标识,成功返回0,失败返回-1
|
||||
int sem_destroy(sem_t *sem); //删除信号量,成功返回0,失败返回-1
|
||||
//进程间通信一般用有名信号量
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <semaphore.h> //有名信号量头文件
|
||||
sem_t *sem_open(const char *name,int oflag,mode_t mode,unsigned int value); //创建信号量,成功返回信号量地址,失败返回SEM_FAILED
|
||||
int sem_close(sem_t *sem); //关闭有名信号量,成功返回0,失败返回-1
|
||||
int sem_unlink(const char *name); //删除有名信号量文件,成功返回0,失败返回-1
|
212
常用脚本/agent.py
Executable file
212
常用脚本/agent.py
Executable file
@ -0,0 +1,212 @@
|
||||
#!/usr/bin/python2
|
||||
# -*- coding:utf-8 -*-
|
||||
#=========================================
|
||||
# Filename : agent.py
|
||||
# Filetype : Python
|
||||
# Author : Colben
|
||||
# Create : 2016-07-28 13:36:04
|
||||
#=========================================
|
||||
|
||||
import os, sys, paramiko, re, csv
|
||||
from optparse import OptionParser
|
||||
from multiprocessing import Process, cpu_count, Lock
|
||||
from time import sleep, time
|
||||
from threading import Thread
|
||||
|
||||
def print_process_result():
|
||||
output_fmt = {}
|
||||
output_fmt['ERROR'] = '\033[31;5mERROR\033[0m'
|
||||
output_fmt['WRONG'] = '\033[33;1mWRONG\033[0m'
|
||||
output_fmt['RIGHT'] = '\033[32mRIGHT\033[0m'
|
||||
output_fmt['LOCAL'] = '\033[32mLOCAL\033[0m'
|
||||
csv_rfp = open('/tmp/agent_result.csv', 'rb')
|
||||
for record in csv.reader(csv_rfp):
|
||||
record[0] = record[0] + '-'*(15-len(record[0]))
|
||||
record[1] = output_fmt[record[1]]
|
||||
print '-> '.join(record)
|
||||
csv_rfp.close()
|
||||
return
|
||||
|
||||
def write_csv(record):
|
||||
process_lock.acquire()
|
||||
csv_writer.writerow(record)
|
||||
process_lock.release()
|
||||
return
|
||||
|
||||
def time_out(record, timeout):
|
||||
detail = record[2]
|
||||
begin_time = int(time())
|
||||
while int(time()) - begin_time < timeout:
|
||||
if detail != record[2]: return
|
||||
sleep(0.5)
|
||||
write_csv(record)
|
||||
os.kill(os.getpid(), 9)
|
||||
return
|
||||
|
||||
def start_timer(record, timeout):
|
||||
if 0 >= options.timeout:
|
||||
return
|
||||
thread_timer = Thread(target = time_out, args = (record, timeout, ))
|
||||
thread_timer.setDaemon(1)
|
||||
thread_timer.start()
|
||||
return
|
||||
|
||||
def ssh_command(ip):
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
record = [ip, 'ERROR', 'CONN TIMEOUT']
|
||||
start_timer(record, 10)
|
||||
try:
|
||||
ssh.connect(ip, port=22, username='root', pkey=ssh_key)
|
||||
#ssh.connect(ip, 22, 'root', 'password')
|
||||
except Exception, e:
|
||||
record[2] = str(e)
|
||||
write_csv(record)
|
||||
return
|
||||
record[2], record[1] = 'TASK TIMEOUT', 'WRONG'
|
||||
command = re.sub(r'IIPP', ip, options.remote_cmd)
|
||||
start_timer(record, options.timeout)
|
||||
stdin, stdout, stderr = ssh.exec_command(command)
|
||||
err = stderr.readlines()
|
||||
out = stdout.readlines()
|
||||
if err:
|
||||
record[2] = ' '.join([x.strip() for x in err])
|
||||
else:
|
||||
record[2], record[1] = ' '.join([x.strip() for x in out]), 'RIGHT'
|
||||
write_csv(record)
|
||||
return
|
||||
|
||||
def ssh_transport(ip):
|
||||
record = [ip, 'ERROR', 'CONN TIMEOUT']
|
||||
start_timer(record, 10)
|
||||
try:
|
||||
t = paramiko.Transport((ip, 22))
|
||||
t.connect(username='root', pkey=ssh_key)
|
||||
#t.connect(username='root', password='password')
|
||||
except Exception, e:
|
||||
record[2] = str(e)
|
||||
write_csv(record)
|
||||
return
|
||||
record[2], record[1] = 'TASK TIMEOUT', 'WRONG'
|
||||
remote_file = re.sub(r'IIPP',ip,os.path.join(options.dst_path, os.path.basename(options.src_file)))
|
||||
start_timer(record, options.timeout)
|
||||
try:
|
||||
sftp = paramiko.SFTPClient.from_transport(t)
|
||||
sftp.put(options.src_file, remote_file)
|
||||
record[2], record[1] = '', 'RIGHT'
|
||||
except Exception, e:
|
||||
record[2] = str(e)
|
||||
write_csv(record)
|
||||
return
|
||||
|
||||
def local_command(ip):
|
||||
command = re.sub(r'IIPP', ip, options.local_cmd)
|
||||
record = [ip, 'WRONG', 'TASK TIMEOUT']
|
||||
start_timer(record, options.timeout)
|
||||
result = os.popen(command)
|
||||
record[2], record[1] = ' '.join([x.strip() for x in result.readlines()]), 'LOCAL'
|
||||
write_csv(record)
|
||||
return
|
||||
|
||||
def check_pool_state(empty=False):
|
||||
while True:
|
||||
print "\033[2J\033[0;0H"
|
||||
for ip, process in process_list.items():
|
||||
if process.is_alive():
|
||||
print 'Handling', ip, '...'
|
||||
else:
|
||||
#print ip, '\033[32mfinished\033[0m'
|
||||
process_list.pop(ip)
|
||||
if not empty and options.process > len(process_list): break
|
||||
if empty and 0 == len(process_list):break
|
||||
sleep(0.5)
|
||||
return
|
||||
|
||||
def start_process_pool():
|
||||
for ip in ips:
|
||||
if options.remote_cmd:
|
||||
sub_process = Process(target=ssh_command, args=(ip,))
|
||||
elif options.src_file:
|
||||
sub_process = Process(target=ssh_transport, args=(ip,))
|
||||
elif options.local_cmd:
|
||||
sub_process = Process(target=local_command, args=(ip,))
|
||||
else:
|
||||
print 'Unknown parameter ...'
|
||||
sys.exit(1)
|
||||
sub_process.start()
|
||||
process_list[ip] = sub_process
|
||||
check_pool_state()
|
||||
check_pool_state(empty=True)
|
||||
return
|
||||
|
||||
def get_hosts(hosts_file):
|
||||
if '/dev/stdin' == hosts_file:
|
||||
print 'Specify no host file, enter ip manually here ...'
|
||||
try:
|
||||
fp = open(hosts_file, 'r')
|
||||
except Exception, e:
|
||||
print 'Failed to read hosts_file,', e
|
||||
sys.exit(1)
|
||||
ips = []
|
||||
for line in fp:
|
||||
matchs = re.match(r'^(10\.\d{1,3}\.\d{1,3}\.\d{1,3})', line)
|
||||
if matchs: ips.append(matchs.group(1))
|
||||
if 0 == len(ips):
|
||||
print 'No ips found, quit ...'
|
||||
return ips
|
||||
|
||||
def initial():
|
||||
global csv_writer, process_lock, ssh_key, ips, csv_fp, process_list
|
||||
try:
|
||||
ssh_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
|
||||
csv_fp = open('/tmp/agent_result.csv', 'wb', 0)
|
||||
csv_writer = csv.writer(csv_fp)
|
||||
except Exception, e:
|
||||
print e
|
||||
sys.exit(1)
|
||||
ips = get_hosts(options.host)
|
||||
process_lock = Lock()
|
||||
process_list = {}
|
||||
return
|
||||
|
||||
def get_options():
|
||||
global options
|
||||
usage = '%s\n\t-R remote-cmd -H ip-file -P process-num -T timeout'%sys.argv[0]
|
||||
usage += '\n\t-S local-file -D remote-dir -H ip-file -P process-num -T timeout'
|
||||
usage += '\n\t-L local-cmd -H ip-file -P process-num -T timeout'
|
||||
usage += '\nThe pattern "IIPP" in options "RSDL" will be replaced by the ip contained in each process.'
|
||||
usage += '\nThe result file is /tmp/agent_result.csv'
|
||||
parser = OptionParser(usage)
|
||||
parser.add_option('-R', '--remote_cmd', action='store', help='Run a shell command in remote servers.')
|
||||
parser.add_option('-S', '--src_file', action='store', help='specify the file to remote servers.')
|
||||
parser.add_option('-D', '--dst_path', action='store', help='specify the path in remote servers.')
|
||||
parser.add_option('-L', '--local_cmd', action='store', help='Run a shell command in localhost.')
|
||||
parser.add_option('-H', '--host', action='store', default='/dev/stdin', help='Specify the file contains ip.')
|
||||
parser.add_option('-P', '--process', action='store', default=cpu_count(), type='int', help='Specify the num of processes.')
|
||||
parser.add_option('-T', '--timeout', action='store', default=0, type='int', help='Specify the seconds of timeout.')
|
||||
options, args = parser.parse_args()
|
||||
for opt in [options.src_file, options.dst_path, options.local_cmd]:
|
||||
if options.remote_cmd and opt:
|
||||
parser.print_help()
|
||||
sys.exit(1)
|
||||
for opt in [options.src_file, options.dst_path]:
|
||||
if options.local_cmd and opt:
|
||||
parser.print_help()
|
||||
sys.exit(1)
|
||||
if options.src_file and not options.dst_path or options.dst_path and not options.src_file:
|
||||
parser.print_help()
|
||||
sys.exit(1)
|
||||
if not options.remote_cmd and not options.src_file and not options.local_cmd:
|
||||
parser.print_help()
|
||||
sys.exit(1)
|
||||
return
|
||||
|
||||
def main():
|
||||
get_options()
|
||||
initial()
|
||||
start_process_pool()
|
||||
print_process_result()
|
||||
return 0
|
||||
|
||||
if '__main__' == __name__:
|
||||
main()
|
61
常用脚本/ipflow
Executable file
61
常用脚本/ipflow
Executable file
@ -0,0 +1,61 @@
|
||||
#!/bin/bash
|
||||
|
||||
type function > /dev/null 2>&1
|
||||
[ 0 -ne $? ] && echo 'Run with bash.' && exit 3
|
||||
[ 1 -lt $# ] && echo "Usage: $0 [seconds]" && exit 1
|
||||
[[ ! "$2" =~ ^[0-9]*$ ]] && "Usage: $0 [seconds]" && exit 2
|
||||
|
||||
CONF='/tmp/ipflow.conf'
|
||||
|
||||
function deal_forward {
|
||||
for ip in $(cat $CONF); do
|
||||
iptables -$1 FORWARD -s $ip -j COLBEN_UPLOAD
|
||||
iptables -$1 FORWARD -d $ip -j COLBEN_DOWNLOAD
|
||||
done
|
||||
}
|
||||
|
||||
function insert_forward {
|
||||
cat /proc/net/arp | grep '^10' | grep -v '00:00:00:00:00:00' | awk '{print $1}' > $CONF
|
||||
deal_forward I
|
||||
}
|
||||
|
||||
function clean_forward {
|
||||
[ ! -f "$CONF" ] && return 0
|
||||
deal_forward D
|
||||
rm -f $CONF
|
||||
}
|
||||
|
||||
function show_result {
|
||||
clear
|
||||
echo 'Download:'
|
||||
iptables -nvxL FORWARD | grep COLBEN_DOWNLOAD | awk "{printf(\"%-4dKB/s:%-10s \n\",\$2/1024/$1,\$9)}" | sort -t: -r
|
||||
echo -e '\033[s\033[1;25HUpload:'
|
||||
line_num=1
|
||||
iptables -nvxL FORWARD | grep COLBEN_UPLOAD | awk "{printf(\"%-4dKB/s:%-10s\n\",\$2/1024/$1,\$8)}" | sort -t: -r | while read line; do
|
||||
let "line_num=1+$line_num"
|
||||
echo -e "\033[$line_num;25H$line"
|
||||
done
|
||||
echo -e '\033[u'
|
||||
sleep $1
|
||||
}
|
||||
|
||||
function quit {
|
||||
clean_forward
|
||||
iptables -X COLBEN_UPLOAD
|
||||
iptables -X COLBEN_DOWNLOAD
|
||||
exit 0
|
||||
}
|
||||
|
||||
iptables -N COLBEN_UPLOAD
|
||||
iptables -N COLBEN_DOWNLOAD
|
||||
|
||||
trap "quit" 2 3 15
|
||||
|
||||
while :; do
|
||||
insert_forward
|
||||
iptables -Z FORWARD
|
||||
show_result ${1:-2}
|
||||
clean_forward
|
||||
sleep 1
|
||||
done
|
||||
|
232
常用脚本/oracle/oracle_mul.py
Executable file
232
常用脚本/oracle/oracle_mul.py
Executable file
@ -0,0 +1,232 @@
|
||||
#!/nmsmw/python/bin/python
|
||||
# -*- encoding: gbk -*-
|
||||
#=========================================
|
||||
# Filename : oracle_mul.py
|
||||
# Filetype : Python
|
||||
# Author : Colben
|
||||
# Create : 2014-12-19 10:40:30
|
||||
#=========================================
|
||||
|
||||
import os, sys, re, cx_Oracle, signal
|
||||
from multiprocessing import Pool, cpu_count
|
||||
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.ZHS16GBK'
|
||||
#os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.AL32UTF8'
|
||||
|
||||
remote_ora = {}#<<FOLD<<
|
||||
#remote_ora['gk'] = 'boya/boya@10.128.75.65:1521/orcl.ntars.com'
|
||||
remote_ora['gk'] = 'ibms/ibms@10.166.64.41:1521/orcl'
|
||||
remote_ora['108'] = 'boya/boya@10.166.64.41:1521/orcl'
|
||||
remote_ora['anhui'] = 'boya/boya@10.152.64.33:1521/orcl.idevelopment.info'
|
||||
remote_ora['beijing'] = 'boya/boya@10.134.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['chongqing'] = 'boya/boya@10.137.64.33:1521/orcl'
|
||||
remote_ora['fujian'] = 'boya/boya@10.162.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['gansu'] = 'boya/boya@10.142.64.33:1521/orcl'
|
||||
remote_ora['guangdong'] = 'boya/boya@10.164.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['guangxi'] = 'boya/boya@10.161.64.33:1521/orcl'
|
||||
remote_ora['guizhou'] = 'boya/boya@10.157.64.33:1521/orcl'
|
||||
remote_ora['hainan'] = 'boya/boya@10.160.64.33:1521/orcl'
|
||||
remote_ora['hebei'] = 'boya/boya@10.141.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['heilong'] = 'boya/boya@10.146.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['henan'] = 'boya/boya@10.151.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['hubei'] = 'boya/boya@10.155.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['hunan'] = 'boya/boya@10.158.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['jiangsu'] = 'boya/boya@10.150.64.33:1521/orclXDB.ntars.com'
|
||||
remote_ora['jiangxi'] = 'boya/boya@10.159.64.33:1521/orcl.info'
|
||||
remote_ora['jilin'] = 'boya/boya@10.147.64.33:1521/orcl'
|
||||
remote_ora['liaoning'] = 'boya/boya@10.143.64.33:1521/orcl.ntars.info'
|
||||
remote_ora['neimeng'] = 'boya/boya@10.135.64.33:1521/orcl'
|
||||
remote_ora['ningxia'] = 'boya3w/boya3w@10.139.64.33:1521/orcl'
|
||||
remote_ora['qinghai'] = 'boya/boya@10.144.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['shandong'] = 'boya/boya@10.154.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['shanghai'] = 'boya/boya@10.163.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['shanxi'] = 'boya/boya@10.140.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['sanxi'] = 'boya/boya@10.145.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['sichuan'] = 'boya/boya@10.138.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['tianjin'] = 'boya/boya@10.148.64.33:1521/orcl'
|
||||
remote_ora['xinjiang'] = 'boya/boya@10.149.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['xizang'] = 'boya/boya@10.136.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['yunnan'] = 'boya/boya@10.156.64.33:1521/orcl'
|
||||
remote_ora['zhejiang'] = 'boya/boya@10.153.64.33:1521/orcl.ntars.com'
|
||||
|
||||
remote_rac = {}
|
||||
#remote_rac['gk'] = 'boya/boya@10.128.75.67:1521/orcl.ntars.com'
|
||||
remote_rac['anhui'] = 'boya/boya@10.152.64.34:1521/orcl.idevelopment.info'
|
||||
remote_rac['beijing'] = 'boya/boya@10.134.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['chongqing'] = 'boya/boya@10.137.64.34:1521/orcl'
|
||||
remote_rac['fujian'] = 'boya/boya@10.162.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['gansu'] = 'boya/boya@10.142.64.34:1521/orcl'
|
||||
remote_rac['guangdong'] = 'boya/boya@10.164.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['guangxi'] = 'boya/boya@10.161.64.34:1521/orcl'
|
||||
remote_rac['guizhou'] = 'boya/boya@10.157.64.34:1521/orcl'
|
||||
remote_rac['hainan'] = 'boya/boya@10.160.64.34:1521/orcl'
|
||||
remote_rac['hebei'] = 'boya/boya@10.141.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['heilong'] = 'boya/boya@10.146.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['henan'] = 'boya/boya@10.151.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['hubei'] = 'boya/boya@10.155.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['hunan'] = 'boya/boya@10.158.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['jiangsu'] = 'boya/boya@10.150.64.34:1521/orclXDB.ntars.com'
|
||||
remote_rac['jiangxi'] = 'boya/boya@10.159.64.34:1521/orcl.info'
|
||||
remote_rac['jilin'] = 'boya/boya@10.147.64.34:1521/orcl'
|
||||
remote_rac['liaoning'] = 'boya/boya@10.143.64.34:1521/orcl.ntars.info'
|
||||
remote_rac['neimeng'] = 'boya/boya@10.135.64.34:1521/orcl'
|
||||
remote_rac['ningxia'] = 'boya3w/boya3w@10.139.64.34:1521/orcl'
|
||||
remote_rac['qinghai'] = 'boya/boya@10.144.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['shandong'] = 'boya/boya@10.154.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['shanghai'] = 'boya/boya@10.163.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['shanxi'] = 'boya/boya@10.140.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['sanxi'] = 'boya/boya@10.145.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['sichuan'] = 'boya/boya@10.138.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['tianjin'] = 'boya/boya@10.148.64.34:1521/orcl'
|
||||
remote_rac['xinjiang'] = 'boya/boya@10.149.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['xizang'] = 'boya/boya@10.136.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['yunnan'] = 'boya/boya@10.156.64.34:1521/orcl'
|
||||
remote_rac['zhejiang'] = 'boya/boya@10.153.64.34:1521/orcl.ntars.com'
|
||||
#>>FOLD>>
|
||||
|
||||
def close_dbs():#<<FOLD<<
|
||||
print 'Terminating db connections ...'
|
||||
for ora, db in accessed_dbs.items():
|
||||
try: db.close()
|
||||
except Exception, e:
|
||||
print 'Close', ora ,'failed:', e
|
||||
sys.exit()
|
||||
#>>FOLD>>
|
||||
|
||||
def handle_one_db(ora):#<<FOLD<<
|
||||
result = []
|
||||
cursor = accessed_dbs[ora].cursor()
|
||||
for sql in sql_list:
|
||||
try:
|
||||
cursor.execute(sql)
|
||||
except Exception, e:
|
||||
result.append((sql + ';', e.__class__.__name__ + ': ' + str(e).strip()))
|
||||
continue
|
||||
try:
|
||||
result.append((sql + ';', cursor.fetchall()))
|
||||
except cx_Oracle.InterfaceError:
|
||||
result.append((sql + ';', 'NULL'))
|
||||
cursor.close()
|
||||
return (ora, result)
|
||||
#>>FOLD>>
|
||||
|
||||
def runsqls_on_alldbs():#<<FOLD<<
|
||||
process_pool = Pool(cpu_count())
|
||||
pool_return = process_pool.map_async(handle_one_db, accessed_dbs.keys())
|
||||
for ora, result in pool_return.get():
|
||||
result_fp.write('Database: ' + ora + '\n')
|
||||
for line in result:
|
||||
result_fp.write('Sql: ' + line[0] + '\n')
|
||||
if type(line[1]) is str:
|
||||
result_fp.write(line[1] + '\n')
|
||||
else:
|
||||
for record in line[1]:
|
||||
result_fp.write(','.join(map(str, record)))
|
||||
result_fp.write('\n')
|
||||
result_fp.write('\n')
|
||||
process_pool.close()
|
||||
process_pool.terminate()
|
||||
return
|
||||
#>>FOLD>>
|
||||
|
||||
def get_fp(filename):#<<FOLD<<
|
||||
global result_fp
|
||||
result_fp = sys.stdout
|
||||
if '' != filename:
|
||||
try:
|
||||
result_fp = open(filename, 'a', 0)
|
||||
except:
|
||||
print 'Open', filename, 'failed, now print it stdout.'
|
||||
return
|
||||
#>>FOLD>>
|
||||
|
||||
def get_sqls(sql):#<<FOLD<<
|
||||
global sql_list
|
||||
sql_list = []
|
||||
if '@' != sql[0]:
|
||||
sql_list.append(sql)
|
||||
else:
|
||||
try:
|
||||
sql_f = open(sql[1:].strip(), 'r')
|
||||
for line in sql_f:
|
||||
sql=line.strip().strip(';')
|
||||
if sql:
|
||||
sql_list.append(sql)
|
||||
sql_f.close()
|
||||
if 0 == len(sql_list):
|
||||
return False
|
||||
except Exception, e:
|
||||
print e.__class__.__name__, ':', e
|
||||
return False
|
||||
return True
|
||||
#>>FOLD>>
|
||||
|
||||
def input_sqls():#<<FOLD<<
|
||||
while True:
|
||||
sql = raw_input('\nSQL>> ')
|
||||
if re.match(r'exit.*', sql.lower()) or re.match(r'quit.*', sql.lower()):
|
||||
close_dbs()
|
||||
try:
|
||||
sql, filename = re.split(r';', sql, 1)
|
||||
except:
|
||||
print 'Wrong, missing ";" in the end.'
|
||||
continue
|
||||
if not get_sqls(sql.strip()):
|
||||
continue
|
||||
get_fp(filename.strip())
|
||||
runsqls_on_alldbs()
|
||||
return
|
||||
#>>FOLD>>
|
||||
|
||||
def access_db(ora):#<<FOLD<<
|
||||
if ora not in remote_ora.keys():
|
||||
return (False, 'Unknown DB')
|
||||
try:
|
||||
db = cx_Oracle.connect(remote_ora[ora])
|
||||
db.autocommit = True
|
||||
return (True, db)
|
||||
except Exception, e:
|
||||
if ora not in remote_rac.keys():
|
||||
return (False, 'Access Failed: ' + str(e))
|
||||
try:
|
||||
db = cx_Oracle.connect(remote_rac[ora])
|
||||
db.autocommit = True
|
||||
return (True, db)
|
||||
except Exception, e:
|
||||
return (False, 'Access Failed: ' + str(e))
|
||||
#>>FOLD>>
|
||||
|
||||
def check_dbs():#<<FOLD<<
|
||||
global accessed_dbs
|
||||
accessed_dbs = {}
|
||||
oras = remote_ora.keys()
|
||||
if 1 != len(sys.argv):
|
||||
oras = sys.argv[1:]
|
||||
for ora in oras:
|
||||
accessed, db = access_db(ora)
|
||||
if not accessed:
|
||||
print ora + ' : ' + db
|
||||
continue
|
||||
accessed_dbs[ora] = db
|
||||
if 0 == len(accessed_dbs):
|
||||
print 'Accessed no dbs, quit ...'
|
||||
sys.exit()
|
||||
return
|
||||
#>>FOLD>>
|
||||
|
||||
def signal_quit(signum, frame):#<<FOLD<<
|
||||
print 'Got quit signal ...'
|
||||
close_dbs()
|
||||
#>>FOLD>>
|
||||
|
||||
def main():#<<FOLD<<
|
||||
check_dbs()
|
||||
input_sqls()
|
||||
return
|
||||
#>>FOLD>>
|
||||
|
||||
if '__main__' == __name__:
|
||||
signal.signal(2, signal_quit)
|
||||
#signal.signal(3, signal_quit)
|
||||
#signal.signal(15, signal_quit)
|
||||
main()
|
||||
|
305
常用脚本/oracle/partition_day.py
Executable file
305
常用脚本/oracle/partition_day.py
Executable file
@ -0,0 +1,305 @@
|
||||
#!/nmsmw/python/bin/python
|
||||
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
import time
|
||||
import datetime
|
||||
import cx_Oracle
|
||||
from multiprocessing import Pool
|
||||
|
||||
remote_ora = {}#<<FOLD<<
|
||||
#remote_ora['gk'] = 'boya/boya@10.128.75.65:1521/orcl.ntars.com'
|
||||
remote_ora['gk'] = 'ibms/ibms@10.166.64.41:1521/orcl'
|
||||
remote_ora['108'] = 'boya/boya@10.166.64.41:1521/orcl'
|
||||
remote_ora['anhui'] = 'boya/boya@10.152.64.33:1521/orcl.idevelopment.info'
|
||||
remote_ora['beijing'] = 'boya/boya@10.134.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['chongqing'] = 'boya/boya@10.137.64.33:1521/orcl'
|
||||
remote_ora['fujian'] = 'boya/boya@10.162.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['gansu'] = 'boya/boya@10.142.64.33:1521/orcl'
|
||||
remote_ora['guangdong'] = 'boya/boya@10.164.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['guangxi'] = 'boya/boya@10.161.64.33:1521/orcl'
|
||||
remote_ora['guizhou'] = 'boya/boya@10.157.64.33:1521/orcl'
|
||||
remote_ora['hainan'] = 'boya/boya@10.160.64.33:1521/orcl'
|
||||
remote_ora['hebei'] = 'boya/boya@10.141.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['heilong'] = 'boya/boya@10.146.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['henan'] = 'boya/boya@10.151.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['hubei'] = 'boya/boya@10.155.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['hunan'] = 'boya/boya@10.158.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['jiangsu'] = 'boya/boya@10.150.64.33:1521/orclXDB.ntars.com'
|
||||
remote_ora['jiangxi'] = 'boya/boya@10.159.64.33:1521/orcl.info'
|
||||
remote_ora['jilin'] = 'boya/boya@10.147.64.33:1521/orcl'
|
||||
remote_ora['liaoning'] = 'boya/boya@10.143.64.33:1521/orcl.ntars.info'
|
||||
remote_ora['neimeng'] = 'boya/boya@10.135.64.33:1521/orcl'
|
||||
remote_ora['ningxia'] = 'boya3w/boya3w@10.139.64.33:1521/orcl'
|
||||
remote_ora['qinghai'] = 'boya/boya@10.144.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['shandong'] = 'boya/boya@10.154.64.33:1521/orcl.ntars.com'
|
||||
#remote_ora['shandong'] = 'ntars/sddb_2008@10.154.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['shanghai'] = 'boya/boya@10.163.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['shanxi'] = 'boya/boya@10.140.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['sanxi'] = 'boya/boya@10.145.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['sichuan'] = 'boya/boya@10.138.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['tianjin'] = 'boya/boya@10.148.64.33:1521/orcl'
|
||||
remote_ora['xinjiang'] = 'boya/boya@10.149.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['xizang'] = 'boya/boya@10.136.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['yunnan'] = 'boya/boya@10.156.64.33:1521/orcl'
|
||||
remote_ora['zhejiang'] = 'boya/boya@10.153.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['ipv6'] = 'boya/boya@10.128.75.6:1521/orcl'
|
||||
#>>FOLD>>
|
||||
|
||||
remote_rac = {}#<<FOLD<<
|
||||
#remote_rac['gk'] = 'boya/boya@10.128.75.67:1521/orcl.ntars.com'
|
||||
remote_rac['anhui'] = 'boya/boya@10.152.64.34:1521/orcl.idevelopment.info'
|
||||
remote_rac['beijing'] = 'boya/boya@10.134.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['chongqing'] = 'boya/boya@10.137.64.34:1521/orcl'
|
||||
remote_rac['fujian'] = 'boya/boya@10.162.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['gansu'] = 'boya/boya@10.142.64.34:1521/orcl'
|
||||
remote_rac['guangdong'] = 'boya/boya@10.164.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['guangxi'] = 'boya/boya@10.161.64.34:1521/orcl'
|
||||
remote_rac['guizhou'] = 'boya/boya@10.157.64.34:1521/orcl'
|
||||
remote_rac['hainan'] = 'boya/boya@10.160.64.34:1521/orcl'
|
||||
remote_rac['hebei'] = 'boya/boya@10.141.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['heilong'] = 'boya/boya@10.146.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['henan'] = 'boya/boya@10.151.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['hubei'] = 'boya/boya@10.155.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['hunan'] = 'boya/boya@10.158.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['jiangsu'] = 'boya/boya@10.150.64.34:1521/orclXDB.ntars.com'
|
||||
remote_rac['jiangxi'] = 'boya/boya@10.159.64.34:1521/orcl.info'
|
||||
remote_rac['jilin'] = 'boya/boya@10.147.64.34:1521/orcl'
|
||||
remote_rac['liaoning'] = 'boya/boya@10.143.64.34:1521/orcl.ntars.info'
|
||||
remote_rac['neimeng'] = 'boya/boya@10.135.64.34:1521/orcl'
|
||||
remote_rac['ningxia'] = 'boya3w/boya3w@10.139.64.34:1521/orcl'
|
||||
remote_rac['qinghai'] = 'boya/boya@10.144.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['shandong'] = 'boya/boya@10.154.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['shanghai'] = 'boya/boya@10.163.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['shanxi'] = 'boya/boya@10.140.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['sanxi'] = 'boya/boya@10.145.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['sichuan'] = 'boya/boya@10.138.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['tianjin'] = 'boya/boya@10.148.64.34:1521/orcl'
|
||||
remote_rac['xinjiang'] = 'boya/boya@10.149.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['xizang'] = 'boya/boya@10.136.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['yunnan'] = 'boya/boya@10.156.64.34:1521/orcl'
|
||||
remote_rac['zhejiang'] = 'boya/boya@10.153.64.34:1521/orcl.ntars.com'
|
||||
#>>FOLD>>
|
||||
|
||||
mtx_tables = {}#<<FOLD<<
|
||||
mtx_tables['MONITOR_FAULT'] = 'COLLECTOR_STATE_SPACE'
|
||||
mtx_tables['MT_COLLECTOR_STATE'] = 'COLLECTOR_STATE_SPACE'
|
||||
mtx_tables['MT_COLLECTOR_DIR'] = 'COLLECTOR_STATE_SPACE'
|
||||
mtx_tables['MTX_PERF_NET'] = 'MTX_PERF_NET'
|
||||
mtx_tables['MTXRPT_EVENT_M'] = 'EVENT_COUNT_SPACE'
|
||||
mtx_tables['MTXRPT_DEVICE_M'] = 'EVENT_COUNT_SPACE'
|
||||
mtx_tables['MTXRPT_EVENTMOBILE_M'] = 'EVENT_COUNT_SPACE'
|
||||
mtx_tables['RPT_MTXMOBILE_DEVICE_M'] = 'EVENT_COUNT_SPACE'
|
||||
mtx_tables['RPT_MTXMOBILE_EVENT_M'] = 'EVENT_COUNT_SPACE'
|
||||
mtx_tables['RPT_MTXMOBILE_IMEI_M'] = 'EVENT_COUNT_SPACE'
|
||||
mtx_tables['RPT_MTXMOBILE_INOUT_H'] = 'EVENT_COUNT_SPACE'
|
||||
mtx_tables['RPT_MTXMOBILE_PROVINCE_H'] = 'EVENT_COUNT_SPACE'
|
||||
mtx_tables['RPT_MTXMOBILE_PROVINCE_M'] = 'EVENT_COUNT_SPACE'
|
||||
mtx_tables['MTX_EVENT_REALTIME'] = 'EVENT_COUNT_SPACE'
|
||||
#>>FOLD>>
|
||||
|
||||
dns_tables = {}#<<FOLD<<
|
||||
dns_tables['DOMAIN_AA'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['DOMAIN_CLIENT_COUNT'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['DOMAIN_COUNT'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['DOMAIN_DNAME_LENGTH'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['DOMAIN_FLUX'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['DOMAIN_OPCODE_COUNT'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['DOMAIN_QUERY_PACKET_LENGTH'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['DOMAIN_IMSI_TOPN'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['DOMAIN_IMSI_TOP100'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['DOMAIN_RA'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['DOMAIN_RCODE'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['DOMAIN_RD'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['DOMAIN_RESPONSE_PACKET_LENGTH'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['DOMAIN_TC'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['DOMAIN_TTL_LENGTH'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['DOMAIN_TYPE_COUNT'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['ERROR_PKT'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['TOPN_CLIENT_BYTE'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['TOPN_CLIENT_PACKET'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['TOPN_DOMAIN_NAME'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['TOPN_NXDOMAIN'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['TOPN_TLD'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['DNSHJK'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['ABNORMAL_BASE_DNS'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['ABNORMAL_SPECIAL_QUEST'] = 'DOMAIN_DATA_SPACE'
|
||||
dns_tables['ABNORMAL_SERVER_QA'] = 'DOMAIN_DATA_SPACE'
|
||||
#>>FOLD>>
|
||||
|
||||
dnspod_tables = {}#<<FOLD<<
|
||||
dnspod_tables['DNSPOD_IP_COUNT'] = 'DOMAIN_DATA_SPACE'
|
||||
dnspod_tables['DNSPOD_IP_FLUX'] = 'DOMAIN_DATA_SPACE'
|
||||
dnspod_tables['DNSPOD_LOCATION_COUNT'] = 'DOMAIN_DATA_SPACE'
|
||||
dnspod_tables['DNSPOD_LOCATION_DOMAIN'] = 'DOMAIN_DATA_SPACE'
|
||||
dnspod_tables['DNSPOD_PCI_COUNT'] = 'DOMAIN_DATA_SPACE'
|
||||
dnspod_tables['DNSPOD_TOP100_DOMAIN'] = 'DOMAIN_DATA_SPACE'
|
||||
dnspod_tables['DNSPOD_TOP_DOMAIN'] = 'DOMAIN_DATA_SPACE'
|
||||
#>>FOLD>>
|
||||
|
||||
vds_tables = {}#<<FOLD<<
|
||||
vds_tables['VDSRPT_DEVICE_M'] = 'EVENT_COUNT_SPACE'
|
||||
vds_tables['VDSRPT_EVENT_M'] = 'EVENT_COUNT_SPACE'
|
||||
#>>FOLD>>
|
||||
|
||||
ntars_tables = {}#<<FOLD<<
|
||||
#ntars_tables['ROUTER_STAT_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['ROUTER_IPSRCPACKET_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['ROUTER_IPDSTBYTE_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['ROUTER_IPDSTPACKET_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['ROUTER_IPCOUNT_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['ROUTER_CPORT_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['CUSTOMER_IPCOUNT_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['CUSTOMER_TOS_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['CUSTOMER_IPSRCBYTE_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['CUSTOMER_IPDSTBYTE_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['CUSTOMER_PEERIPDSTBYTE_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['CUSTOMER_PEERIPDSTPACKET_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['FILTER_REPORT_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['ROUTER_PROTOCOL_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['CUSTOMER_PROTOCOL_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['CUSTOMER_SRCPORT_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['CUSTOMER_IPDSTPACKET_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['CUSTOMER_DSTPORT_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['CUSTOMER_IPSRCPACKET_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['ROUTER_REGION_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['CUSTOMER_PEERIPSRCPACKET_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['CUSTOMER_CPORT_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['ROUTER_SRCPORT_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['CUSTOMER_PEERIPSRCBYTE_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['ROUTER_TOS_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['ROUTER_DSTPORT_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['ROUTER_IPSRCBYTE_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['CUSTOMER_REGION_H'] = 'REPORT_H_SPACE'
|
||||
#ntars_tables['RG_CUSTOMER_SRCPORT_H'] = 'RG_CS_SRCPORT_H_TS'
|
||||
ntars_tables['ROUTER_STAT_M'] = 'REPORT_M_SPACE'
|
||||
ntars_tables['ROUTER_IPDSTBYTE_M'] = 'REPORT_M_SPACE'
|
||||
ntars_tables['ROUTER_IPCOUNT_M'] = 'REPORT_M_SPACE'
|
||||
ntars_tables['ROUTER_CPORT_M'] = 'REPORT_M_SPACE'
|
||||
ntars_tables['ROUTER_PROTOCOL_M'] = 'REPORT_M_SPACE'
|
||||
ntars_tables['ROUTER_REGION_M'] = 'REPORT_M_SPACE'
|
||||
ntars_tables['ROUTER_SRCPORT_M'] = 'REPORT_M_SPACE'
|
||||
ntars_tables['ROUTER_DSTPORT_M'] = 'REPORT_M_SPACE'
|
||||
ntars_tables['ROUTER_IPSRCBYTE_M'] = 'REPORT_M_SPACE'
|
||||
#>>FOLD>>
|
||||
|
||||
topn_tables = {}#<<FOLD<<
|
||||
topn_tables['TOPN_CLIENT_BYTE'] = 'DOMAIN_DATA_SPACE'
|
||||
topn_tables['TOPN_CLIENT_PACKET'] = 'DOMAIN_DATA_SPACE'
|
||||
topn_tables['TOPN_DOMAIN_NAME'] = 'DOMAIN_DATA_SPACE'
|
||||
topn_tables['TOPN_NXDOMAIN'] = 'DOMAIN_DATA_SPACE'
|
||||
topn_tables['TOPN_TLD'] = 'DOMAIN_DATA_SPACE'
|
||||
#>>FOLD>>
|
||||
|
||||
extra_tables = {}
|
||||
extra_tables['ERROR_PKT'] = 'DOMAIN_DATA_SPACE'
|
||||
extra_tables['MTXRPT_EVENT_M'] = 'DOMAIN_DATA_SPACE'
|
||||
extra_tables['DOMAIN_IMSI_TOPN'] = 'DOMAIN_DATA_SPACE'
|
||||
extra_tables['ABNORMAL_BASE_DNS'] = 'DOMAIN_DATA_SPACE'
|
||||
extra_tables['ABNORMAL_SERVER_QA'] = 'DOMAIN_DATA_SPACE'
|
||||
extra_tables['ABNORMAL_SPECIAL_QUEST'] = 'DOMAIN_DATA_SPACE'
|
||||
extra_tables['DNSPOD_PCI_COUNT'] = 'DOMAIN_DATA_SPACE'
|
||||
extra_tables['DNSPOD_TOP100_DOMAIN'] = 'DOMAIN_DATA_SPACE'
|
||||
extra_tables['DNSPOD_TOP_DOMAIN'] = 'DOMAIN_DATA_SPACE'
|
||||
extra_tables['DNSPOD_IP_COUNT'] = 'DOMAIN_DATA_SPACE'
|
||||
extra_tables['DNSPOD_LOCATION_COUNT'] = 'DOMAIN_DATA_SPACE'
|
||||
extra_tables['DNSHJK'] = 'DOMAIN_DATA_SPACE'
|
||||
|
||||
def StringToTime(strtime):#<<FOLD<<
|
||||
t_tuple = time.strptime(strtime,"%Y%m%d")
|
||||
return int(time.mktime(t_tuple))
|
||||
#>>FOLD>>
|
||||
|
||||
def gen_sqls(operate, date):#<<FOLD<<
|
||||
#tables = dict(dict(dict(mtx_tables, **dns_tables), **dnspod_tables), **vds_tables)
|
||||
#tables = dict(dns_tables, **dnspod_tables)
|
||||
tables = mtx_tables
|
||||
sqls = []
|
||||
if 'add' == operate:
|
||||
date1 = StringToTime(date)
|
||||
for table in tables:
|
||||
sqls.append('alter table %s add partition PART_%s values less than(%s) tablespace %s'%(table, date, date1, tables[table]))
|
||||
elif 'drop' == operate:
|
||||
for table in tables:
|
||||
sqls.append('alter table %s drop partition PART_%s'%(table, date))
|
||||
else:
|
||||
print 'Wrong operation on table partitions, now quit.'
|
||||
return sqls
|
||||
#>>FOLD>>
|
||||
|
||||
def ora_init(ora):#<<FOLD<<
|
||||
if ora in remote_ora.keys():
|
||||
#print ' ------>', ora, '<------'
|
||||
try:
|
||||
db = cx_Oracle.connect(remote_ora[ora])
|
||||
db.autocommit = True
|
||||
#print 'Connect to oracle server successfully ...'
|
||||
except:
|
||||
if ora in remote_rac.keys():
|
||||
#print 'Time out, now try to connect the rac ...'
|
||||
try:
|
||||
db = cx_Oracle.connect(remote_rac[ora])
|
||||
db.autocommit = True
|
||||
#print 'Connect to oracle server successfully ...'
|
||||
except:
|
||||
#print 'Error, Failed to connect oracle', ora
|
||||
return
|
||||
else:
|
||||
#print 'Error, Failed to connect oracle', ora
|
||||
return
|
||||
else:
|
||||
#print 'Error, Wrong oracle', ora
|
||||
return
|
||||
return db
|
||||
#>>FOLD>>
|
||||
|
||||
def operate_partitions(ora):#<<FOLD<<
|
||||
db = ora_init(ora)
|
||||
if not db:
|
||||
print '\n\033[31m %s Connect failed.\033[0m'%ora
|
||||
return (ora, 'Connect failed')
|
||||
cur = db.cursor()
|
||||
month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
||||
for day in range(0, month[int(sys.argv[2])%100-1]):
|
||||
date = str(int(sys.argv[2])*100+1+day)
|
||||
sqls = gen_sqls(sys.argv[1], date)
|
||||
for sql in sqls:
|
||||
try:
|
||||
cur.execute(sql)
|
||||
sys.stdout.write('.')
|
||||
sys.stdout.flush()
|
||||
except Exception, e:
|
||||
matched = 0
|
||||
ignore_errors = ['ORA-00942', 'ORA-02149']
|
||||
for error in ignore_errors:
|
||||
if error in str(e).upper():
|
||||
matched = 1
|
||||
sys.stdout.write('.')
|
||||
sys.stdout.flush()
|
||||
break
|
||||
if not matched:
|
||||
print "\n\033[31mRUN \"%s\" ON %s failed:%s\033[0m"%(sql, ora, str(e))
|
||||
cur.close()
|
||||
db.close()
|
||||
print '\n\033[32m %s Complete.\033[0m'%ora
|
||||
return (ora, 'Complete')
|
||||
#>>FOLD>>
|
||||
|
||||
def main():#<<FOLD<<
|
||||
if 0 == len(sys.argv[3:]):
|
||||
oras = remote_ora.keys()
|
||||
else:
|
||||
oras = sys.argv[3:]
|
||||
process_pool = Pool(8)
|
||||
result = process_pool.map_async(operate_partitions, oras)
|
||||
for res in result.get():
|
||||
print res
|
||||
#>>FOLD>>
|
||||
|
||||
if __name__ == '__main__':
|
||||
if 2 < len(sys.argv) and sys.argv[2].isdigit():
|
||||
main()
|
||||
else:
|
||||
print 'Usage: %s {add|drop} 201603 [pc1, pc2 ...]'%sys.argv[0]
|
||||
|
187
常用脚本/oracle/partition_month.py
Executable file
187
常用脚本/oracle/partition_month.py
Executable file
@ -0,0 +1,187 @@
|
||||
#!/nmsmw/python/bin/python
|
||||
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
import time
|
||||
import datetime
|
||||
import cx_Oracle
|
||||
from multiprocessing import Pool
|
||||
|
||||
remote_ora = {}#<<FOLD<<
|
||||
#remote_ora['gk'] = 'boya/boya@10.128.75.65:1521/orcl.ntars.com'
|
||||
#remote_ora['gk'] = 'ibms/ibms@10.166.64.41:1521/orcl'
|
||||
remote_ora['108'] = 'boya/boya@10.166.64.41:1521/orcl'
|
||||
remote_ora['anhui'] = 'boya/boya@10.152.64.33:1521/orcl.idevelopment.info'
|
||||
remote_ora['beijing'] = 'boya/boya@10.134.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['chongqing'] = 'boya/boya@10.137.64.33:1521/orcl'
|
||||
remote_ora['fujian'] = 'boya/boya@10.162.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['gansu'] = 'boya/boya@10.142.64.33:1521/orcl'
|
||||
remote_ora['guangdong'] = 'boya/boya@10.164.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['guangxi'] = 'boya/boya@10.161.64.33:1521/orcl'
|
||||
remote_ora['guizhou'] = 'boya/boya@10.157.64.33:1521/orcl'
|
||||
remote_ora['hainan'] = 'boya/boya@10.160.64.33:1521/orcl'
|
||||
remote_ora['hebei'] = 'boya/boya@10.141.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['heilong'] = 'boya/boya@10.146.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['henan'] = 'boya/boya@10.151.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['hubei'] = 'boya/boya@10.155.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['hunan'] = 'boya/boya@10.158.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['jiangsu'] = 'boya/boya@10.150.64.33:1521/orclXDB.ntars.com'
|
||||
remote_ora['jiangxi'] = 'boya/boya@10.159.64.33:1521/orcl.info'
|
||||
remote_ora['jilin'] = 'boya/boya@10.147.64.33:1521/orcl'
|
||||
remote_ora['liaoning'] = 'boya/boya@10.143.64.33:1521/orcl.ntars.info'
|
||||
remote_ora['neimeng'] = 'boya/boya@10.135.64.33:1521/orcl'
|
||||
remote_ora['ningxia'] = 'boya3w/boya3w@10.139.64.33:1521/orcl'
|
||||
remote_ora['qinghai'] = 'boya/boya@10.144.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['shandong'] = 'boya/boya@10.154.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['shanghai'] = 'boya/boya@10.163.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['shanxi'] = 'boya/boya@10.140.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['sanxi'] = 'boya/boya@10.145.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['sichuan'] = 'boya/boya@10.138.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['tianjin'] = 'boya/boya@10.148.64.33:1521/orcl'
|
||||
remote_ora['xinjiang'] = 'boya/boya@10.149.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['xizang'] = 'boya/boya@10.136.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['yunnan'] = 'boya/boya@10.156.64.33:1521/orcl'
|
||||
remote_ora['zhejiang'] = 'boya/boya@10.153.64.33:1521/orcl.ntars.com'
|
||||
|
||||
remote_rac = {}
|
||||
#remote_rac['gk'] = 'boya/boya@10.128.75.67:1521/orcl.ntars.com'
|
||||
remote_rac['anhui'] = 'boya/boya@10.152.64.34:1521/orcl.idevelopment.info'
|
||||
remote_rac['beijing'] = 'boya/boya@10.134.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['chongqing'] = 'boya/boya@10.137.64.34:1521/orcl'
|
||||
remote_rac['fujian'] = 'boya/boya@10.162.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['gansu'] = 'boya/boya@10.142.64.34:1521/orcl'
|
||||
remote_rac['guangdong'] = 'boya/boya@10.164.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['guangxi'] = 'boya/boya@10.161.64.34:1521/orcl'
|
||||
remote_rac['guizhou'] = 'boya/boya@10.157.64.34:1521/orcl'
|
||||
remote_rac['hainan'] = 'boya/boya@10.160.64.34:1521/orcl'
|
||||
remote_rac['hebei'] = 'boya/boya@10.141.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['heilong'] = 'boya/boya@10.146.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['henan'] = 'boya/boya@10.151.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['hubei'] = 'boya/boya@10.155.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['hunan'] = 'boya/boya@10.158.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['jiangsu'] = 'boya/boya@10.150.64.34:1521/orclXDB.ntars.com'
|
||||
remote_rac['jiangxi'] = 'boya/boya@10.159.64.34:1521/orcl.info'
|
||||
remote_rac['jilin'] = 'boya/boya@10.147.64.34:1521/orcl'
|
||||
remote_rac['liaoning'] = 'boya/boya@10.143.64.34:1521/orcl.ntars.info'
|
||||
remote_rac['neimeng'] = 'boya/boya@10.135.64.34:1521/orcl'
|
||||
remote_rac['ningxia'] = 'boya3w/boya3w@10.139.64.34:1521/orcl'
|
||||
remote_rac['qinghai'] = 'boya/boya@10.144.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['shandong'] = 'boya/boya@10.154.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['shanghai'] = 'boya/boya@10.163.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['shanxi'] = 'boya/boya@10.140.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['sanxi'] = 'boya/boya@10.145.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['sichuan'] = 'boya/boya@10.138.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['tianjin'] = 'boya/boya@10.148.64.34:1521/orcl'
|
||||
remote_rac['xinjiang'] = 'boya/boya@10.149.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['xizang'] = 'boya/boya@10.136.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['yunnan'] = 'boya/boya@10.156.64.34:1521/orcl'
|
||||
remote_rac['zhejiang'] = 'boya/boya@10.153.64.34:1521/orcl.ntars.com'
|
||||
#>>FOLD>>
|
||||
|
||||
app_tables = {}#<<FOLD<<
|
||||
app_tables['APPRPT_APPSTORE_D'] = 'APPRPT_SPACE'
|
||||
app_tables['APPRPT_APP_USING_D'] = 'APPRPT_SPACE'
|
||||
app_tables['APPRPT_DEVICE_BAND_D'] = 'APPRPT_SPACE'
|
||||
app_tables['APPRPT_DEVICE_BAND_M'] = 'APPRPT_SPACE'
|
||||
app_tables['APPRPT_DEVICE_TYPE_D'] = 'APPRPT_SPACE'
|
||||
app_tables['APPRPT_DEVICE_TYPE_M'] = 'APPRPT_SPACE'
|
||||
app_tables['APPRPT_DOWNLOAD_D'] = 'APPRPT_SPACE'
|
||||
app_tables['APPRPT_SPECIAL_USING_D'] = 'APPRPT_SPACE'
|
||||
#>>FOLD>>
|
||||
|
||||
def StringToTime(strtime):#<<FOLD<<
|
||||
t_tuple = time.strptime(str(strtime),"%Y%m%d")
|
||||
return int(time.mktime(t_tuple))
|
||||
#>>FOLD>>
|
||||
|
||||
def gen_sqls(operate, date):#<<FOLD<<
|
||||
tables = app_tables
|
||||
sqls = []
|
||||
if 'add' == operate:
|
||||
date1 = StringToTime(date)
|
||||
for table in tables:
|
||||
sqls.append('alter table %s add partition PART_%s values less than(%s) tablespace %s'%(table, date/100, date1, tables[table]))
|
||||
elif 'drop' == operate:
|
||||
for table in tables:
|
||||
sqls.append('alter table %s drop partition PART_%s'%(table, date/100))
|
||||
else:
|
||||
print 'Wrong operation on table partitions, now quit.'
|
||||
return
|
||||
return sqls
|
||||
#>>FOLD>>
|
||||
|
||||
def ora_init(ora):#<<FOLD<<
|
||||
if ora in remote_ora.keys():
|
||||
#print ' ------>', ora, '<------'
|
||||
try:
|
||||
db = cx_Oracle.connect(remote_ora[ora])
|
||||
db.autocommit = True
|
||||
#print 'Connect to oracle server successfully ...'
|
||||
except:
|
||||
if ora in remote_rac.keys():
|
||||
#print 'Time out, now try to connect the rac ...'
|
||||
try:
|
||||
db = cx_Oracle.connect(remote_rac[ora])
|
||||
db.autocommit = True
|
||||
#print 'Connect to oracle server successfully ...'
|
||||
except:
|
||||
#print 'Error, Failed to connect oracle', ora
|
||||
return
|
||||
else:
|
||||
#print 'Error, Failed to connect oracle', ora
|
||||
return
|
||||
else:
|
||||
#print 'Error, Wrong oracle', ora
|
||||
return
|
||||
return db
|
||||
#>>FOLD>>
|
||||
|
||||
def operate_partitions(ora):#<<FOLD<<
|
||||
db = ora_init(ora)
|
||||
if not db:
|
||||
print '\n\033[31m %s Connect failed.\033[0m'%ora
|
||||
return (ora, 'Connect failed')
|
||||
cur = db.cursor()
|
||||
for month in range(0, 12):
|
||||
date = (int(sys.argv[2])*100+1+month)*100+1
|
||||
sqls = gen_sqls(sys.argv[1], date)
|
||||
for sql in sqls:
|
||||
try:
|
||||
cur.execute(sql)
|
||||
sys.stdout.write('.')
|
||||
sys.stdout.flush()
|
||||
except Exception, e:
|
||||
matched = 0
|
||||
ignore_errors = ['ORA-00942', 'ORA-02149']
|
||||
for error in ignore_errors:
|
||||
if error in str(e).upper():
|
||||
matched = 1
|
||||
sys.stdout.write('.')
|
||||
sys.stdout.flush()
|
||||
break
|
||||
if not matched:
|
||||
print "\n\033[31mRUN %s ON %s failed:%s\033[0m"%(sql, ora, e)
|
||||
cur.close()
|
||||
db.close()
|
||||
print '\n\033[32m %s Complete.\033[0m'%ora
|
||||
return (ora, 'Complete')
|
||||
#>>FOLD>>
|
||||
|
||||
def main():#<<FOLD<<
|
||||
if 0 == len(sys.argv[3:]):
|
||||
oras = remote_ora.keys()
|
||||
else:
|
||||
oras = sys.argv[3:]
|
||||
process_pool = Pool(8)
|
||||
result = process_pool.map_async(operate_partitions, oras)
|
||||
for res in result.get():
|
||||
print res
|
||||
#>>FOLD>>
|
||||
|
||||
if __name__ == '__main__':
|
||||
if 2 < len(sys.argv) and sys.argv[2].isdigit():
|
||||
main()
|
||||
else:
|
||||
print 'Usage: %s {add|drop} 2016 [pc1, pc2 ...]'%sys.argv[0]
|
||||
|
188
常用脚本/oracle/partition_year.py
Executable file
188
常用脚本/oracle/partition_year.py
Executable file
@ -0,0 +1,188 @@
|
||||
#!/nmsmw/python/bin/python
|
||||
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
import time
|
||||
import datetime
|
||||
import cx_Oracle
|
||||
from multiprocessing import Pool
|
||||
|
||||
remote_ora = {}#<<FOLD<<
|
||||
#remote_ora['gk'] = 'boya/boya@10.128.75.65:1521/orcl.ntars.com'
|
||||
#remote_ora['gk'] = 'ibms/ibms@10.166.64.41:1521/orcl'
|
||||
remote_ora['108'] = 'boya/boya@10.166.64.41:1521/orcl'
|
||||
remote_ora['anhui'] = 'boya/boya@10.152.64.33:1521/orcl.idevelopment.info'
|
||||
remote_ora['beijing'] = 'boya/boya@10.134.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['chongqing'] = 'boya/boya@10.137.64.33:1521/orcl'
|
||||
remote_ora['fujian'] = 'boya/boya@10.162.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['gansu'] = 'boya/boya@10.142.64.33:1521/orcl'
|
||||
remote_ora['guangdong'] = 'boya/boya@10.164.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['guangxi'] = 'boya/boya@10.161.64.33:1521/orcl'
|
||||
remote_ora['guizhou'] = 'boya/boya@10.157.64.33:1521/orcl'
|
||||
remote_ora['hainan'] = 'boya/boya@10.160.64.33:1521/orcl'
|
||||
remote_ora['hebei'] = 'boya/boya@10.141.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['heilong'] = 'boya/boya@10.146.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['henan'] = 'boya/boya@10.151.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['hubei'] = 'boya/boya@10.155.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['hunan'] = 'boya/boya@10.158.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['jiangsu'] = 'boya/boya@10.150.64.33:1521/orclXDB.ntars.com'
|
||||
remote_ora['jiangxi'] = 'boya/boya@10.159.64.33:1521/orcl.info'
|
||||
remote_ora['jilin'] = 'boya/boya@10.147.64.33:1521/orcl'
|
||||
remote_ora['liaoning'] = 'boya/boya@10.143.64.33:1521/orcl.ntars.info'
|
||||
remote_ora['neimeng'] = 'boya/boya@10.135.64.33:1521/orcl'
|
||||
remote_ora['ningxia'] = 'boya3w/boya3w@10.139.64.33:1521/orcl'
|
||||
remote_ora['qinghai'] = 'boya/boya@10.144.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['shandong'] = 'boya/boya@10.154.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['shanghai'] = 'boya/boya@10.163.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['shanxi'] = 'boya/boya@10.140.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['sanxi'] = 'boya/boya@10.145.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['sichuan'] = 'boya/boya@10.138.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['tianjin'] = 'boya/boya@10.148.64.33:1521/orcl'
|
||||
remote_ora['xinjiang'] = 'boya/boya@10.149.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['xizang'] = 'boya/boya@10.136.64.33:1521/orcl.ntars.com'
|
||||
remote_ora['yunnan'] = 'boya/boya@10.156.64.33:1521/orcl'
|
||||
remote_ora['zhejiang'] = 'boya/boya@10.153.64.33:1521/orcl.ntars.com'
|
||||
|
||||
remote_rac = {}
|
||||
#remote_rac['gk'] = 'boya/boya@10.128.75.67:1521/orcl.ntars.com'
|
||||
remote_rac['anhui'] = 'boya/boya@10.152.64.34:1521/orcl.idevelopment.info'
|
||||
remote_rac['beijing'] = 'boya/boya@10.134.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['chongqing'] = 'boya/boya@10.137.64.34:1521/orcl'
|
||||
remote_rac['fujian'] = 'boya/boya@10.162.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['gansu'] = 'boya/boya@10.142.64.34:1521/orcl'
|
||||
remote_rac['guangdong'] = 'boya/boya@10.164.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['guangxi'] = 'boya/boya@10.161.64.34:1521/orcl'
|
||||
remote_rac['guizhou'] = 'boya/boya@10.157.64.34:1521/orcl'
|
||||
remote_rac['hainan'] = 'boya/boya@10.160.64.34:1521/orcl'
|
||||
remote_rac['hebei'] = 'boya/boya@10.141.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['heilong'] = 'boya/boya@10.146.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['henan'] = 'boya/boya@10.151.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['hubei'] = 'boya/boya@10.155.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['hunan'] = 'boya/boya@10.158.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['jiangsu'] = 'boya/boya@10.150.64.34:1521/orclXDB.ntars.com'
|
||||
remote_rac['jiangxi'] = 'boya/boya@10.159.64.34:1521/orcl.info'
|
||||
remote_rac['jilin'] = 'boya/boya@10.147.64.34:1521/orcl'
|
||||
remote_rac['liaoning'] = 'boya/boya@10.143.64.34:1521/orcl.ntars.info'
|
||||
remote_rac['neimeng'] = 'boya/boya@10.135.64.34:1521/orcl'
|
||||
remote_rac['ningxia'] = 'boya3w/boya3w@10.139.64.34:1521/orcl'
|
||||
remote_rac['qinghai'] = 'boya/boya@10.144.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['shandong'] = 'boya/boya@10.154.64.34:1521/orclXDB.ntars.com'
|
||||
remote_rac['shanghai'] = 'boya/boya@10.163.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['shanxi'] = 'boya/boya@10.140.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['sanxi'] = 'boya/boya@10.145.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['sichuan'] = 'boya/boya@10.138.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['tianjin'] = 'boya/boya@10.148.64.34:1521/orcl'
|
||||
remote_rac['xinjiang'] = 'boya/boya@10.149.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['xizang'] = 'boya/boya@10.136.64.34:1521/orcl.ntars.com'
|
||||
remote_rac['yunnan'] = 'boya/boya@10.156.64.34:1521/orcl'
|
||||
remote_rac['zhejiang'] = 'boya/boya@10.153.64.34:1521/orcl.ntars.com'
|
||||
#>>FOLD>>
|
||||
|
||||
vds_tables = {}#<<FOLD<<
|
||||
vds_tables['VDSRPT_DOMAIN_DOWNLOAD'] = 'VDS_REPORT_SPACE'
|
||||
vds_tables['VDSRPT_PROGRAM_DOWNLOAD'] = 'VDS_REPORT_SPACE'
|
||||
vds_tables['VDSRPT_SIP_DOWNLOAD'] = 'VDS_REPORT_SPACE'
|
||||
vds_tables['VDSRPT_URL_DOWNLOAD'] = 'VDS_REPORT_SPACE'
|
||||
vds_tables['VDSRPT_VIRUSFALIMY_DOWNLOAD'] = 'VDS_REPORT_SPACE'
|
||||
vds_tables['VDSRPT_VIRUSTYPE_DOWNLOAD'] = 'VDS_REPORT_SPACE'
|
||||
vds_tables['VDSRPT_VIRUSTYPE_VISIT'] = 'VDS_REPORT_SPACE'
|
||||
vds_tables['VDSRPT_VIRUS_DOWNLOAD'] = 'VDS_REPORT_SPACE'
|
||||
vds_tables['VDSRPT_VIRUS_NEW'] = 'VDS_REPORT_SPACE'
|
||||
vds_tables['VDSRPT_VIRUS_VISIT'] = 'VDS_REPORT_SPACE'
|
||||
#>>FOLD>>
|
||||
|
||||
def StringToTime(strtime):#<<FOLD<<
|
||||
t_tuple = time.strptime(str(strtime),"%Y%m%d")
|
||||
return int(time.mktime(t_tuple))
|
||||
#>>FOLD>>
|
||||
|
||||
def gen_sqls(operate, date):#<<FOLD<<
|
||||
tables = vds_tables
|
||||
sqls = []
|
||||
if 'add' == operate:
|
||||
date1 = StringToTime(date)
|
||||
for table in tables:
|
||||
sqls.append('alter table %s add partition PART_%s values less than(%s) tablespace %s'%(table, date/10000, date1, tables[table]))
|
||||
elif 'drop' == operate:
|
||||
for table in tables:
|
||||
sqls.append('alter table %s drop partition PART_%s'%(table, date/10000))
|
||||
else:
|
||||
print 'Wrong operation on table partitions, now quit.'
|
||||
return
|
||||
return sqls
|
||||
#>>FOLD>>
|
||||
|
||||
def ora_init(ora):#<<FOLD<<
|
||||
if ora in remote_ora.keys():
|
||||
#print ' ------>', ora, '<------'
|
||||
try:
|
||||
db = cx_Oracle.connect(remote_ora[ora])
|
||||
db.autocommit = True
|
||||
#print 'Connect to oracle server successfully ...'
|
||||
except:
|
||||
if ora in remote_rac.keys():
|
||||
#print 'Time out, now try to connect the rac ...'
|
||||
try:
|
||||
db = cx_Oracle.connect(remote_rac[ora])
|
||||
db.autocommit = True
|
||||
#print 'Connect to oracle server successfully ...'
|
||||
except:
|
||||
#print 'Error, Failed to connect oracle', ora
|
||||
return
|
||||
else:
|
||||
#print 'Error, Failed to connect oracle', ora
|
||||
return
|
||||
else:
|
||||
#print 'Error, Wrong oracle', ora
|
||||
return
|
||||
return db
|
||||
#>>FOLD>>
|
||||
|
||||
def operate_partitions(ora):#<<FOLD<<
|
||||
db = ora_init(ora)
|
||||
if not db:
|
||||
print '\n\033[31m %s Connect failed.\033[0m'%ora
|
||||
return (ora, 'Connect failed')
|
||||
cur = db.cursor()
|
||||
date = (int(sys.argv[2])*100+1)*100+1
|
||||
sqls = gen_sqls(sys.argv[1], date)
|
||||
for sql in sqls:
|
||||
try:
|
||||
cur.execute(sql)
|
||||
sys.stdout.write('.')
|
||||
sys.stdout.flush()
|
||||
except Exception, e:
|
||||
matched = 0
|
||||
ignore_errors = ['ORA-00942', 'ORA-02149']
|
||||
for error in ignore_errors:
|
||||
if error in str(e).upper():
|
||||
matched = 1
|
||||
sys.stdout.write('.')
|
||||
sys.stdout.flush()
|
||||
break
|
||||
if not matched:
|
||||
print "\n\033[31mRUN %s ON %s failed:%s\033[0m"%(sql, ora, e)
|
||||
cur.close()
|
||||
db.close()
|
||||
print '\n\033[32m %s Complete.\033[0m'%ora
|
||||
return (ora, 'Complete')
|
||||
#>>FOLD>>
|
||||
|
||||
def main():#<<FOLD<<
|
||||
if 0 == len(sys.argv[3:]):
|
||||
oras = remote_ora.keys()
|
||||
else:
|
||||
oras = sys.argv[3:]
|
||||
process_pool = Pool(8)
|
||||
result = process_pool.map_async(operate_partitions, oras)
|
||||
for res in result.get():
|
||||
print res
|
||||
#>>FOLD>>
|
||||
|
||||
if __name__ == '__main__':
|
||||
if 2 < len(sys.argv) and sys.argv[2].isdigit():
|
||||
main()
|
||||
else:
|
||||
print 'Usage: %s {add|drop} 2017 [pc1, pc2 ...]'%sys.argv[0]
|
||||
|
65
常用脚本/oracle/table_partition.cfg
Normal file
65
常用脚本/oracle/table_partition.cfg
Normal file
@ -0,0 +1,65 @@
|
||||
[day-180-2]
|
||||
MONITOR_FAULT = COLLECTOR_STATE_SPACE
|
||||
MT_COLLECTOR_STATE = COLLECTOR_STATE_SPACE
|
||||
MT_COLLECTOR_DIR = COLLECTOR_STATE_SPACE
|
||||
MTX_PERF_NET = MTX_PERF_NET
|
||||
MTXRPT_EVENT_M = EVENT_COUNT_SPACE
|
||||
MTXRPT_DEVICE_M = EVENT_COUNT_SPACE
|
||||
MTXRPT_EVENTMOBILE_M = EVENT_COUNT_SPACE
|
||||
RPT_MTXMOBILE_DEVICE_M = EVENT_COUNT_SPACE
|
||||
RPT_MTXMOBILE_EVENT_M = EVENT_COUNT_SPACE
|
||||
RPT_MTXMOBILE_IMEI_M = EVENT_COUNT_SPACE
|
||||
RPT_MTXMOBILE_INOUT_H = EVENT_COUNT_SPACE
|
||||
RPT_MTXMOBILE_PROVINCE_H = EVENT_COUNT_SPACE
|
||||
RPT_MTXMOBILE_PROVINCE_M = EVENT_COUNT_SPACE
|
||||
MTX_EVENT_REALTIME = EVENT_COUNT_SPACE
|
||||
VDSRPT_DEVICE_M = EVENT_COUNT_SPACE
|
||||
VDSRPT_EVENT_M = EVENT_COUNT_SPACE
|
||||
|
||||
[day-90-2]
|
||||
DOMAIN_AA = DOMAIN_DATA_SPACE
|
||||
DOMAIN_CLIENT_COUNT = DOMAIN_DATA_SPACE
|
||||
DOMAIN_COUNT = DOMAIN_DATA_SPACE
|
||||
DOMAIN_DNAME_LENGTH = DOMAIN_DATA_SPACE
|
||||
DOMAIN_FLUX = DOMAIN_DATA_SPACE
|
||||
DOMAIN_OPCODE_COUNT = DOMAIN_DATA_SPACE
|
||||
DOMAIN_QUERY_PACKET_LENGTH = DOMAIN_DATA_SPACE
|
||||
DOMAIN_IMSI_TOPN = DOMAIN_DATA_SPACE
|
||||
DOMAIN_IMSI_TOP100 = DOMAIN_DATA_SPACE
|
||||
DOMAIN_RA = DOMAIN_DATA_SPACE
|
||||
DOMAIN_RCODE = DOMAIN_DATA_SPACE
|
||||
DOMAIN_RD = DOMAIN_DATA_SPACE
|
||||
DOMAIN_RESPONSE_PACKET_LENGTH = DOMAIN_DATA_SPACE
|
||||
DOMAIN_TC = DOMAIN_DATA_SPACE
|
||||
DOMAIN_TTL_LENGTH = DOMAIN_DATA_SPACE
|
||||
DOMAIN_TYPE_COUNT = DOMAIN_DATA_SPACE
|
||||
ERROR_PKT = DOMAIN_DATA_SPACE
|
||||
DNSHJK = DOMAIN_DATA_SPACE
|
||||
ABNORMAL_BASE_DNS = DOMAIN_DATA_SPACE
|
||||
ABNORMAL_SPECIAL_QUEST = DOMAIN_DATA_SPACE
|
||||
ABNORMAL_SERVER_QA = DOMAIN_DATA_SPACE
|
||||
DNSPOD_IP_COUNT = DOMAIN_DATA_SPACE
|
||||
DNSPOD_IP_FLUX = DOMAIN_DATA_SPACE
|
||||
DNSPOD_LOCATION_COUNT = DOMAIN_DATA_SPACE
|
||||
DNSPOD_LOCATION_DOMAIN = DOMAIN_DATA_SPACE
|
||||
DNSPOD_PCI_COUNT = DOMAIN_DATA_SPACE
|
||||
DNSPOD_TOP100_DOMAIN = DOMAIN_DATA_SPACE
|
||||
DNSPOD_TOP_DOMAIN = DOMAIN_DATA_SPACE
|
||||
|
||||
[day-30-2]
|
||||
TOPN_CLIENT_BYTE = DOMAIN_DATA_SPACE
|
||||
TOPN_CLIENT_PACKET = DOMAIN_DATA_SPACE
|
||||
TOPN_DOMAIN_NAME = DOMAIN_DATA_SPACE
|
||||
TOPN_NXDOMAIN = DOMAIN_DATA_SPACE
|
||||
TOPN_TLD = DOMAIN_DATA_SPACE
|
||||
|
||||
[month-1-2]
|
||||
APPRPT_APPSTORE_D = APPRPT_SPACE
|
||||
APPRPT_DOWNLOAD_D = APPRPT_SPACE
|
||||
APPRPT_APP_USING_D = APPRPT_SPACE
|
||||
APPRPT_DEVICE_BAND_D = APPRPT_SPACE
|
||||
APPRPT_DEVICE_BAND_M = APPRPT_SPACE
|
||||
APPRPT_DEVICE_TYPE_D = APPRPT_SPACE
|
||||
APPRPT_DEVICE_TYPE_M = APPRPT_SPACE
|
||||
APPRPT_SPECIAL_USING_D = APPRPT_SPACE
|
||||
|
177
常用脚本/oracle/table_partition.py
Executable file
177
常用脚本/oracle/table_partition.py
Executable file
@ -0,0 +1,177 @@
|
||||
#!/usr/bin/python2
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import cx_Oracle
|
||||
import ConfigParser
|
||||
from time import sleep
|
||||
|
||||
HOST_CONF = 'host.conf'
|
||||
TABLE_CONF = 'table_partition.cfg'
|
||||
|
||||
SQL = {}
|
||||
SQL_HEAD = '''-- <<FOLD<<
|
||||
DECLARE
|
||||
A VARCHAR2(16);
|
||||
B NUMBER(8);
|
||||
NOW DATE;
|
||||
FIRST_DAY DATE;
|
||||
SQL_ VARCHAR2(256);
|
||||
TIME_STAMP NUMBER(16);
|
||||
TABLE_NAME VARCHAR2(32);
|
||||
LOW_PART_BOUND EXCEPTION;
|
||||
NO_EXIST_TABLE EXCEPTION;
|
||||
NO_EXIST_PART EXCEPTION;
|
||||
THE_ONLY_PART EXCEPTION;
|
||||
PRAGMA EXCEPTION_INIT(LOW_PART_BOUND, -14074);
|
||||
PRAGMA EXCEPTION_INIT(NO_EXIST_TABLE, -00942);
|
||||
PRAGMA EXCEPTION_INIT(NO_EXIST_PART, -02149);
|
||||
PRAGMA EXCEPTION_INIT(THE_ONLY_PART, -14083);
|
||||
PROCEDURE IGNORE_ERROR_EXECUTE_SQL(SQL_ IN VARCHAR2) IS
|
||||
BEGIN
|
||||
EXECUTE IMMEDIATE SQL_;
|
||||
EXCEPTION
|
||||
WHEN LOW_PART_BOUND OR NO_EXIST_TABLE OR NO_EXIST_PART OR THE_ONLY_PART THEN
|
||||
NULL;
|
||||
END;
|
||||
BEGIN
|
||||
'''
|
||||
# >>FOLD>>
|
||||
SQL['day'] = '''-- <<FOLD<<
|
||||
FOR A IN
|
||||
(SELECT PARTITION_NAME
|
||||
FROM USER_TAB_PARTITIONS
|
||||
WHERE TABLE_NAME = '{table}' AND
|
||||
PARTITION_NAME != 'PART_1' AND
|
||||
PARTITION_NAME <= 'PART_' || TO_CHAR(SYSDATE-{n_keep}, 'yyyyMMdd'))
|
||||
LOOP
|
||||
SQL_ := 'ALTER TABLE {table} DROP PARTITION ' || A.PARTITION_NAME;
|
||||
IGNORE_ERROR_EXECUTE_SQL(SQL_);
|
||||
END LOOP;
|
||||
B := 2;
|
||||
NOW := SYSDATE;
|
||||
WHILE B < (2 + {n_create})
|
||||
LOOP
|
||||
FIRST_DAY := TO_DATE(TO_CHAR(NOW + B, 'YYYYMMDD') || '-00:00:00', 'YYYYMMDD-HH24:MI:SS');
|
||||
SELECT (FIRST_DAY - TO_DATE('19700101-08:00:00', 'YYYYMMDD-HH24:MI:SS')) * 24 * 60 * 60 INTO TIME_STAMP FROM DUAL;
|
||||
SQL_ := 'ALTER TABLE {table} ADD PARTITION PART_' || TO_CHAR(FIRST_DAY, 'YYYYMMDD') || ' VALUES LESS THAN(' || TIME_STAMP || ') TABLESPACE {tablespace}';
|
||||
IGNORE_ERROR_EXECUTE_SQL(SQL_);
|
||||
B := B + 1;
|
||||
END LOOP;
|
||||
'''
|
||||
# >>FOLD>>
|
||||
SQL['month'] = '''-- <<FOLD<<
|
||||
FOR A IN
|
||||
(SELECT PARTITION_NAME
|
||||
FROM USER_TAB_PARTITIONS
|
||||
WHERE TABLE_NAME = '{table}' AND
|
||||
PARTITION_NAME != 'PART_1' AND
|
||||
PARTITION_NAME <= 'PART_' || TO_CHAR(ADD_MONTHS(SYSDATE, -{n_keep}), 'yyyyMM'))
|
||||
LOOP
|
||||
SQL_ := 'ALTER TABLE {table} DROP PARTITION ' || A.PARTITION_NAME;
|
||||
IGNORE_ERROR_EXECUTE_SQL(SQL_);
|
||||
END LOOP;
|
||||
B := 2;
|
||||
NOW := SYSDATE;
|
||||
WHILE B < (2 + {n_create})
|
||||
LOOP
|
||||
FIRST_DAY := TO_DATE(TO_CHAR(ADD_MONTHS(NOW, B), 'YYYYMM') || '01-00:00:00', 'YYYYMMDD-HH24:MI:SS');
|
||||
SELECT (FIRST_DAY - TO_DATE('19700101-08:00:00', 'YYYYMMDD-HH24:MI:SS')) * 24 * 60 * 60 INTO TIME_STAMP FROM DUAL;
|
||||
SQL_ := 'ALTER TABLE {table} ADD PARTITION PART_' || TO_CHAR(FIRST_DAY, 'YYYYMM') || ' VALUES LESS THAN(' || TIME_STAMP || ') TABLESPACE {tablespace}';
|
||||
IGNORE_ERROR_EXECUTE_SQL(SQL_);
|
||||
B := B + 1;
|
||||
END LOOP;
|
||||
'''
|
||||
# >>FOLD>>
|
||||
SQL['year'] = '''-- <<FOLD<<
|
||||
FOR A IN
|
||||
(SELECT PARTITION_NAME
|
||||
FROM USER_TAB_PARTITIONS
|
||||
WHERE TABLE_NAME = '{table}' AND
|
||||
PARTITION_NAME != 'PART_1' AND
|
||||
PARTITION_NAME <= 'PART_' || (TO_CHAR(SYSDATE, 'yyyy') - {n_keep}))
|
||||
LOOP
|
||||
SQL_ := 'ALTER TABLE {table} DROP PARTITION ' || A.PARTITION_NAME;
|
||||
IGNORE_ERROR_EXECUTE_SQL(SQL_);
|
||||
END LOOP;
|
||||
B := 2;
|
||||
NOW := SYSDATE;
|
||||
WHILE B < (2 + {n_create})
|
||||
LOOP
|
||||
FIRST_DAY := TO_DATE(TO_CHAR(ADD_MONTHS(NOW, B*12), 'YYYY') || '0101-00:00:00', 'YYYYMMDD-HH24:MI:SS');
|
||||
SELECT (FIRST_DAY - TO_DATE('19700101-08:00:00', 'YYYYMMDD HH24:MI:SS')) * 24 * 60 * 60 INTO TIME_STAMP FROM DUAL;
|
||||
SQL_ := 'ALTER TABLE {table} ADD PARTITION PART_' || TO_CHAR(FIRST_DAY, 'YYYY') || ' VALUES LESS THAN(' || TIME_STAMP || ') TABLESPACE {tablespace}';
|
||||
IGNORE_ERROR_EXECUTE_SQL(SQL_);
|
||||
B := B + 1;
|
||||
END LOOP;
|
||||
'''
|
||||
# >>FOLD>>
|
||||
SQL_FOOT = '''# <<FOLD<<
|
||||
END;
|
||||
'''
|
||||
# >>FOLD>>
|
||||
|
||||
def generate_plsql(config):# <<FOLD<<
|
||||
plsql = SQL_HEAD
|
||||
for section in config.sections():
|
||||
part_type, n_keep, n_create = section.split('-')
|
||||
n_keep = int(n_keep)
|
||||
n_create = int(n_create)
|
||||
for table in map(lambda x: x.upper(), config.options(section)):
|
||||
plsql += SQL[part_type].format(table=table, n_keep=n_keep, n_create=n_create, tablespace=config.get(section, table))
|
||||
plsql += SQL_FOOT
|
||||
return plsql
|
||||
# >>FOLD>>
|
||||
|
||||
def operate_plsql(db, plsql):# <<FOLD<<
|
||||
#print plsql
|
||||
cur = db.cursor()
|
||||
cur.execute(plsql)
|
||||
#try:
|
||||
# cur.execute(sql)
|
||||
#except Exception, e:
|
||||
# matched = 0
|
||||
# ignore_errors = ['ORA-00942', 'ORA-02149']
|
||||
# for error in ignore_errors:
|
||||
# if error in str(e).upper():
|
||||
# matched = 1
|
||||
# sys.stdout.write('.')
|
||||
# sys.stdout.flush()
|
||||
# break
|
||||
# if not matched:
|
||||
# print "\n\033[31mRUN \"%s\" ON %s failed:%s\033[0m"%(sql, ora, str(e))
|
||||
cur.close()
|
||||
db.close()
|
||||
return
|
||||
# >>FOLD>>
|
||||
|
||||
def connect_db():# <<FOLD<<
|
||||
config = ConfigParser.ConfigParser()
|
||||
config.read(HOST_CONF)
|
||||
user = config.get('db', 'user')
|
||||
pswd = config.get('db', 'passwd')
|
||||
orcl = config.get('db', 'db')
|
||||
try:
|
||||
db = cx_Oracle.connect(user, pswd, orcl)
|
||||
db.autocommit = True
|
||||
except Exception, e:
|
||||
print 'Connect Oracle failed: %s'%str(e)
|
||||
return False
|
||||
return db
|
||||
# >>FOLD>>
|
||||
|
||||
def main():# <<FOLD<<
|
||||
global config
|
||||
while 1:
|
||||
config = ConfigParser.ConfigParser()
|
||||
config.read(TABLE_CONF)
|
||||
plsql = generate_plsql(config)
|
||||
db = connect_db()
|
||||
if db: operate_plsql(db, plsql)
|
||||
sleep(14400)
|
||||
# >>FOLD>>
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
18
常用脚本/python/msg_log.ini
Normal file
18
常用脚本/python/msg_log.ini
Normal file
@ -0,0 +1,18 @@
|
||||
[_msg_log_]
|
||||
level=info
|
||||
file=/path/to/default.log
|
||||
stderr=1
|
||||
syslog=0
|
||||
center=1
|
||||
sysID=019
|
||||
subsysID=13
|
||||
file_size=300
|
||||
file_counter=3
|
||||
|
||||
[table_partition]
|
||||
file=/path/to/table_partition.log
|
||||
stderr=1
|
||||
level=info
|
||||
file_size=100
|
||||
file_counter=6
|
||||
|
206
常用脚本/python/rising_log.py
Executable file
206
常用脚本/python/rising_log.py
Executable file
@ -0,0 +1,206 @@
|
||||
#!/usr/bin/env python
|
||||
#-*- encoding: gbk -*-
|
||||
#
|
||||
|
||||
import ConfigParser
|
||||
import logging
|
||||
import time, os
|
||||
from logging.handlers import RotatingFileHandler
|
||||
import platform
|
||||
|
||||
#global
|
||||
log_msg_root = '_msg_log_'
|
||||
|
||||
#log_stderr_format = '$RESET$COLOR<%(levelname)-5s><%(name)s:%(process)d:%(threadName)s><%(filename)s:%(lineno)d>%(message)s'
|
||||
log_stderr_format = '$COLOR$BOLD<%(levelname)-5s><%(name)s:%(process)d:%(threadName)s>$RESET$COLOR<%(filename)s:%(lineno)d>%(message)s'
|
||||
#log_stderr_format = "$COLOR%(levelname)s $RESET %(asctime)s $BOLD$COLOR%(name)s$RESET %(message)s"
|
||||
|
||||
if platform.system() == 'Windows':
|
||||
log_file_format = '<%(levelname)s><%(name)s:%(process)d>%(message)s'
|
||||
else:
|
||||
log_file_format = '%(asctime)s <%(levelname)-5s><%(name)s:%(process)d:%(threadName)s><%(filename)s:%(lineno)d>%(message)s'
|
||||
|
||||
|
||||
# for colorful stderr output, copy from http://stackoverflow.com/questions/384076/how-can-i-make-the-python-logging-output-to-be-colored
|
||||
# by rrt
|
||||
|
||||
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
|
||||
RESET_SEQ = "\033[0m"
|
||||
#COLOR_SEQ = "\033[1;%dm" # color & bold
|
||||
COLOR_SEQ = "\033[0;%dm" # color only
|
||||
BOLD_SEQ = "\033[1m"
|
||||
|
||||
COLORS={
|
||||
'WARNING':WHITE,
|
||||
'INFO':YELLOW,
|
||||
'DEBUG':GREEN,
|
||||
'CRITICAL':YELLOW,
|
||||
'ERROR':RED,
|
||||
'RED':RED,
|
||||
'GREEN':GREEN,
|
||||
'YELLOW':YELLOW,
|
||||
'BLUE':BLUE,
|
||||
'MAGENTA':MAGENTA,
|
||||
'CYAN':CYAN,
|
||||
'WHITE':WHITE,}
|
||||
"""
|
||||
COLORS = {
|
||||
'INFO': YELLOW,
|
||||
'DEBUG': GREEN,
|
||||
'ERROR': RED }
|
||||
"""
|
||||
|
||||
class ColoredFormatter(logging.Formatter):
|
||||
def __init__(self, *args, **kwargs):
|
||||
logging.Formatter.__init__(self, *args, **kwargs)
|
||||
|
||||
def format(self, record):
|
||||
levelname = record.levelname
|
||||
color = COLOR_SEQ % (30 + COLORS[levelname])
|
||||
message = logging.Formatter.format(self, record)
|
||||
#print "1--" + repr(message)
|
||||
message = message.replace("$RESET", RESET_SEQ)\
|
||||
.replace("$BOLD", BOLD_SEQ)\
|
||||
.replace("$COLOR", color)
|
||||
#print "2--" + repr(message)
|
||||
#for k,v in COLORS.items():
|
||||
# message = message.replace("$" + k, COLOR_SEQ % (v+30))\
|
||||
# .replace("$BG" + k, COLOR_SEQ % (v+40))\
|
||||
# .replace("$BG-" + k, COLOR_SEQ % (v+40))
|
||||
#print "3--" + repr(message)
|
||||
return message + RESET_SEQ
|
||||
|
||||
|
||||
|
||||
#This class copy form liup's blog http://hi.baidu.com/dalier/blog/item/68419d64b98604faf63654e4.html
|
||||
#by rrt on 2010-12-3
|
||||
|
||||
class DateRotatingFileHandler(RotatingFileHandler):
|
||||
def __init__(self , filename , mode='a' , maxBytes=0, backupCount=0, encoding=None):
|
||||
self.current = time.strftime("%Y%m%d" , time.localtime(time.time()))
|
||||
self.path = os.path.dirname(filename)
|
||||
self.filename = os.path.basename(filename)
|
||||
newdir = os.path.join(self.path , self.current)
|
||||
softlink = os.path.join(self.path, 'current')
|
||||
if not os.access(newdir , os.X_OK):
|
||||
os.mkdir(newdir)
|
||||
try:
|
||||
os.symlink(newdir ,softlink)
|
||||
except:
|
||||
os.system("rm -f " + softlink)
|
||||
os.symlink(newdir ,softlink)
|
||||
newfile = os.path.join(newdir , self.filename)
|
||||
RotatingFileHandler.__init__(self, newfile , mode, maxBytes , backupCount , encoding)
|
||||
|
||||
def doRollover(self):
|
||||
#print "doRollover , current=%s , filename=%s"%(self.current , self.baseFilename)
|
||||
self.stream.close()
|
||||
self.current = time.strftime("%Y%m%d" , time.localtime(time.time()))
|
||||
|
||||
#Modified by rrt on 2010-12-4 for a log path bug: /'20101204'
|
||||
#repr() is not needed , time.strftime() return a string not a integer
|
||||
#newdir = os.path.join(self.path , repr(self.current))
|
||||
newdir = os.path.join(self.path , self.current)
|
||||
if not os.access(newdir , os.X_OK):
|
||||
os.mkdir(newdir)
|
||||
self.baseFilename = os.path.join(newdir , self.filename)
|
||||
|
||||
if self.encoding:
|
||||
self.stream = codecs.open(self.baseFilename, 'w', self.encoding)
|
||||
else:
|
||||
self.stream = open(self.baseFilename, 'w')
|
||||
|
||||
def shouldRollover(self, record):
|
||||
if RotatingFileHandler.shouldRollover(self , record):
|
||||
RotatingFileHandler.doRollover(self)
|
||||
|
||||
t = time.strftime("%Y%m%d" , time.localtime(time.time()))
|
||||
if (cmp(self.current , t) < 0) :
|
||||
return 1
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def init_log(prefix, cfg_file, type,level):
|
||||
global log_msg_root, log_file_format, log_stderr_format
|
||||
|
||||
cf = ConfigParser.ConfigParser()
|
||||
cf.read(cfg_file)
|
||||
config = {}
|
||||
|
||||
# we can use
|
||||
# = cf.get(prefix, 'level').split('#')[0].strip()
|
||||
# to enable "#" to comment behind a value string.
|
||||
|
||||
try:
|
||||
config['level'] = cf.get(prefix, 'level')
|
||||
except:
|
||||
config['level'] = cf.get(log_msg_root, 'level')
|
||||
|
||||
try:
|
||||
config['file'] = cf.get(prefix, 'file')
|
||||
except:
|
||||
path = cf.get(log_msg_root, 'file')
|
||||
path = os.path.dirname(path)
|
||||
config['file'] = os.path.join(path, prefix+'.log')
|
||||
|
||||
try:
|
||||
config['stderr'] = cf.getint(prefix, 'stderr')
|
||||
except:
|
||||
config['stderr'] = cf.getint(log_msg_root, 'stderr')
|
||||
|
||||
try:
|
||||
config['syslog'] = cf.getint(prefix, 'syslog')
|
||||
except:
|
||||
config['syslog'] = cf.getint(log_msg_root, 'syslog')
|
||||
|
||||
try:
|
||||
config['file_size'] = cf.getint(prefix, 'file_size')
|
||||
except:
|
||||
config['file_size'] = cf.getint(log_msg_root, 'file_size')
|
||||
|
||||
try:
|
||||
config['file_cnt'] = cf.getint(prefix, 'file_counter')
|
||||
except:
|
||||
config['file_cnt'] = cf.getint(log_msg_root, 'file_counter')
|
||||
|
||||
print config
|
||||
|
||||
#new a root logger
|
||||
logger = logging.getLogger(prefix)
|
||||
#set root logger level
|
||||
if config['level'] == 'debug':
|
||||
logger.setLevel(logging.DEBUG)
|
||||
elif config['level'] == 'info':
|
||||
logger.setLevel(logging.INFO)
|
||||
else:
|
||||
logger.setLevel(logging.ERROR)
|
||||
#new a log file handle
|
||||
log_handler = DateRotatingFileHandler(config['file'], mode="a", maxBytes = config['file_size']*1024*1024, backupCount=config['file_cnt'])
|
||||
#set log file handle format
|
||||
formatter = logging.Formatter(log_file_format, datefmt='%Y-%m-%d %H:%M:%S')# '%F %T') jython not work
|
||||
log_handler.setFormatter(formatter)
|
||||
#add to root logger
|
||||
logger.addHandler(log_handler)
|
||||
#new a stderr logger if need
|
||||
if not config['stderr']:
|
||||
return logger
|
||||
else:
|
||||
|
||||
formatter = ColoredFormatter(log_stderr_format)
|
||||
#formatter = logging.Formatter(log_stderr_format)
|
||||
log_handler = logging.StreamHandler()
|
||||
log_handler.setFormatter(formatter)
|
||||
logger.addHandler(log_handler)
|
||||
return logger
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
global logger
|
||||
|
||||
logger = init_log('test_log_lib', 'msg_log.ini', 0, 0)
|
||||
logger.debug('debug rrrrrrrrrrrrrrrrrrrrrrrrrrrt')
|
||||
logger.info('info rrrrrrrrrrrrrrrrrrrrrrrrrrrt')
|
||||
logger.error('error rrrrrrrrrrrrrrrrrrrrrrrrrrrt')
|
||||
|
5
常用脚本/python/rollback_log.cfg
Normal file
5
常用脚本/python/rollback_log.cfg
Normal file
@ -0,0 +1,5 @@
|
||||
[conf]
|
||||
interval=43200
|
||||
day=7
|
||||
used=1024
|
||||
log_dir=/log/
|
131
常用脚本/python/rollback_log.py
Executable file
131
常用脚本/python/rollback_log.py
Executable file
@ -0,0 +1,131 @@
|
||||
#!/usr/bin/python2
|
||||
|
||||
from socket import *
|
||||
import time
|
||||
import sys
|
||||
import os
|
||||
import re
|
||||
import signal
|
||||
import ConfigParser
|
||||
import struct
|
||||
import subprocess
|
||||
import shutil
|
||||
from subprocess import Popen
|
||||
|
||||
#sys.path.append('/lib/')
|
||||
import rising_log
|
||||
|
||||
ROLLBACK_CONF = 'rollback_log.cfg'
|
||||
|
||||
### init log ###
|
||||
global logger
|
||||
logger = rising_log.init_log('rollback_log', 'msg_log.ini', 0, 0)
|
||||
|
||||
def _sig_INT_handle(signum, frame):
|
||||
logger.error("SIGINT recieve, exit()")
|
||||
sys.exit()
|
||||
|
||||
def dir_status(log_dir,used):
|
||||
|
||||
syscmd = 'du -s %s'%(log_dir)
|
||||
|
||||
output = Popen(syscmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
|
||||
|
||||
output_err = output.stderr.readlines()
|
||||
output_out = output.stdout.readlines()
|
||||
|
||||
if len(output_err) > 0:
|
||||
for line in output_err:
|
||||
logger.error("%s",line)
|
||||
logger.error("syscmd:%s failed",syscmd)
|
||||
return 0
|
||||
|
||||
if len(output_out) > 0:
|
||||
for line in output_out:
|
||||
if log_dir in line:
|
||||
dir_used = int(line.split("\t")[0])/1024
|
||||
|
||||
logger.info("dir used:%d, max:%d",dir_used,used)
|
||||
|
||||
if dir_used > used:
|
||||
logger.info("disk used:%d to max:%d",dir_used,used)
|
||||
return -1
|
||||
return 0
|
||||
|
||||
def check_dir(data_dir):
|
||||
if not os.path.exists(data_dir):
|
||||
logger.error("no dir:%s,then create dir",data_dir)
|
||||
os.makedirs(data_dir)
|
||||
|
||||
def init_rollback_config(config_file):
|
||||
rollback_config = {}
|
||||
try:
|
||||
config = ConfigParser.ConfigParser()
|
||||
config.read(config_file)
|
||||
|
||||
rollback_config['interval'] = config.get('conf', 'interval')
|
||||
rollback_config['day'] = config.get('conf', 'day')
|
||||
rollback_config['used'] = config.get('conf', 'used')
|
||||
rollback_config['log_dir'] = config.get('conf', 'log_dir')
|
||||
|
||||
except Exception,msg:
|
||||
logger.error(str(msg).strip())
|
||||
sys.exit(1)
|
||||
|
||||
return rollback_config
|
||||
|
||||
|
||||
def rollback_log(log_dir,day,used):
|
||||
|
||||
file_list = []
|
||||
logdir_list =[]
|
||||
|
||||
file_list = os.listdir(log_dir)
|
||||
|
||||
if len(file_list) <= 0:
|
||||
logger.debug("no log data in dir:%s",log_dir)
|
||||
return 0
|
||||
|
||||
file_list.sort(reverse=True)
|
||||
dir_num = 0
|
||||
for i in file_list:
|
||||
logdir_name = os.path.join(log_dir, i)
|
||||
if os.path.isdir(logdir_name):
|
||||
dir_num = dir_num + 1
|
||||
if dir_num > day:
|
||||
logger.info("dir_num:%d, maxnum:%d",dir_num,day)
|
||||
logger.info("delete log dir:%s",logdir_name)
|
||||
shutil.rmtree(logdir_name)
|
||||
|
||||
else:
|
||||
logdir_list.append(logdir_name)
|
||||
|
||||
logdir_list.sort()
|
||||
del logdir_list[-1]
|
||||
|
||||
for i in logdir_list:
|
||||
if dir_status(log_dir,used) < 0 :
|
||||
logger.info("delete log dir:%s",i)
|
||||
shutil.rmtree(i)
|
||||
else:
|
||||
break
|
||||
|
||||
return 1
|
||||
|
||||
def main():
|
||||
signal.signal(signal.SIGINT, _sig_INT_handle)
|
||||
|
||||
while(1):
|
||||
rollback_config = init_rollback_config(ROLLBACK_CONF)
|
||||
|
||||
sleep_interval = int(rollback_config['interval'])
|
||||
log_dir = rollback_config['log_dir']
|
||||
day = int(rollback_config['day'])
|
||||
used = int(rollback_config['used'])
|
||||
|
||||
rollback_log(log_dir,day,used)
|
||||
time.sleep(sleep_interval)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
67
常用脚本/shell/check_net.sh
Executable file
67
常用脚本/shell/check_net.sh
Executable file
@ -0,0 +1,67 @@
|
||||
#!/bin/bash
|
||||
#=========================================
|
||||
# Filename : check_net.sh
|
||||
# Author : Colben
|
||||
# Create : 2017-12-04 11:04
|
||||
#=========================================
|
||||
|
||||
PROCS=()
|
||||
|
||||
function Quit {
|
||||
[ -n "$1" ] && echo -e "\033[31;1mERROR: $1 !\033[0m"
|
||||
exit 1
|
||||
}
|
||||
|
||||
function StopPing {
|
||||
for pid in ${!PROCS[@]}; do
|
||||
kill $pid
|
||||
done
|
||||
Quit
|
||||
}
|
||||
|
||||
function PingAddr {
|
||||
local oldState=
|
||||
local newState=
|
||||
local alterTime=
|
||||
while :; do
|
||||
ping -w4 -c2 -q $1 > /dev/null 2>&1
|
||||
newState=$?
|
||||
[ "$oldState" = "$newState" ] || alterTime="$(date +'%H:%M')"
|
||||
oldState=$newState
|
||||
if [ '0' = "$newState" ]; then
|
||||
echo "\033[32;1m connected\033[0m on $alterTime." > $1
|
||||
sleep 8
|
||||
else
|
||||
echo "\033[31;1m lost\033[0m on $alterTime!" > $1
|
||||
sleep 4
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# start
|
||||
[ 0 -eq $# ] && Quit "No hosts found"
|
||||
mkdir -p /tmp/check_net || Quit "create dir failed"
|
||||
cd /tmp/check_net || Quit "change dir failed"
|
||||
trap 2 3 15 "StopPing"
|
||||
|
||||
for addr in $@; do
|
||||
PingAddr $addr &
|
||||
PROCS[$!]="$addr"
|
||||
done
|
||||
|
||||
while :; do
|
||||
for pid in ${!PROCS[@]}; do
|
||||
if [ ! -f /proc/$pid/stat ]; then
|
||||
PingAddr ${PROCS[$pid]} &
|
||||
PROC[$!]=PROC[$pid]
|
||||
unset PROC[$pid]
|
||||
fi
|
||||
done
|
||||
clear
|
||||
for f in $(ls); do
|
||||
echo -e "$f:$(cat $f)"
|
||||
done
|
||||
sleep 4
|
||||
done
|
||||
|
||||
|
55
常用脚本/shell/install.sh
Executable file
55
常用脚本/shell/install.sh
Executable file
@ -0,0 +1,55 @@
|
||||
#!/bin/bash
|
||||
|
||||
# usage
|
||||
USAGE="Usage: $0"
|
||||
|
||||
# installation processes
|
||||
PROCESSES='
|
||||
CheckFun1
|
||||
CheckFun2
|
||||
InitFun1
|
||||
InitFun2
|
||||
'
|
||||
|
||||
# report error and quit
|
||||
function Quit {
|
||||
[ -n "$1" ] && echo "$1!" > /dev/stderr
|
||||
echo -e "\033[31;1mInstallaion failed!\033[0m\n"
|
||||
exit 1
|
||||
}
|
||||
|
||||
function CheckFun1 {
|
||||
echo ''
|
||||
}
|
||||
|
||||
function CheckFun2 {
|
||||
echo ''
|
||||
}
|
||||
|
||||
function InitFun1 {
|
||||
echo ''
|
||||
}
|
||||
|
||||
function InitFun2 {
|
||||
echo ''
|
||||
}
|
||||
|
||||
# execute every function in PROCESSES
|
||||
function Main {
|
||||
for process in $PROCESSES; do
|
||||
echo -e "\n\033[33;1m- $func ...\033[0m"
|
||||
$process
|
||||
done
|
||||
echo -e "\033[32;1mInstall successfully!\033[0m\n"
|
||||
}
|
||||
|
||||
# start here
|
||||
# check root privilege
|
||||
[ 0 -ne $UID ] && Quit "Can't run without ROOT"
|
||||
# check arguments
|
||||
[ 0 -ne $# ] && Quit "$USAGE"
|
||||
# cd to script dir
|
||||
cd $(dirname $0) || Quit
|
||||
# install
|
||||
Main
|
||||
|
19
常用脚本/shell/processes
Normal file
19
常用脚本/shell/processes
Normal file
@ -0,0 +1,19 @@
|
||||
#!/bin/bash
|
||||
|
||||
SERVICES=(
|
||||
'rabbitmq-server'
|
||||
'redis-server'
|
||||
'mongod'
|
||||
'mysql'
|
||||
'tomcat7'
|
||||
'nginx'
|
||||
)
|
||||
|
||||
DAEMONS=(
|
||||
'/a/b/c -f -g'
|
||||
'd -e iiii'
|
||||
'/j/k'
|
||||
'llll'
|
||||
'/mm/nn -a -b -c'
|
||||
)
|
||||
|
180
常用脚本/shell/rc.all
Executable file
180
常用脚本/shell/rc.all
Executable file
@ -0,0 +1,180 @@
|
||||
#!/bin/bash
|
||||
|
||||
function Quit {
|
||||
[ -n "$1" ] && echo -e "\n\033[31;1mERROR: $1\033[0m!\n"
|
||||
exit 3
|
||||
}
|
||||
|
||||
function Spreadout {
|
||||
local i=
|
||||
local service=
|
||||
local daemon=
|
||||
echo "System ${ROOT_DIR##*/} processes ..."
|
||||
for service in ${SERVICES[@]}; do
|
||||
echo "Service: \"$service\""
|
||||
done
|
||||
for i in ${!DAEMONS[@]}; do
|
||||
daemon="${DAEMONS[i]}"
|
||||
echo "Daemon: \"$daemon\""
|
||||
done
|
||||
exit 0
|
||||
}
|
||||
|
||||
function ShowState {
|
||||
local name=$1
|
||||
local err=$2
|
||||
local total=$3
|
||||
local style=$4
|
||||
if [ '0' = "$total" ]; then
|
||||
echo -e "$name \033[32${style}mnot found\033[0m ..."
|
||||
return 254
|
||||
elif [ '0' = "$err" ]; then
|
||||
echo -e "$name \033[32${style}mrunning\033[0m ..."
|
||||
return 0
|
||||
elif [ -z "$total" -o "$total" = "$err" ]; then
|
||||
echo -e "$name \033[33${style}mstopped\033[0m!"
|
||||
return 1
|
||||
else
|
||||
echo -e "$name \033[31${style}merror\033[0m!!"
|
||||
return 255
|
||||
fi
|
||||
}
|
||||
|
||||
function HandleServices {
|
||||
local service=
|
||||
for service in ${SERVICES[@]}; do
|
||||
echo "===> ${1}ing \"$service\" ..."
|
||||
systemctl $1 $service
|
||||
sleep 0.4
|
||||
done
|
||||
}
|
||||
|
||||
function HandleDaemons {
|
||||
local i=
|
||||
local daemon=
|
||||
for i in ${!DAEMONS[@]}; do
|
||||
daemon="${DAEMONS[i]}"
|
||||
echo "===> ${1}ing \"$daemon\" ..."
|
||||
$WATCH_PROC $1 $daemon
|
||||
sleep 0.4
|
||||
done
|
||||
}
|
||||
|
||||
function CheckServices {
|
||||
local err=0
|
||||
local service=
|
||||
for service in ${SERVICES[@]}; do
|
||||
systemctl status $service > /dev/null 2>/dev/null
|
||||
ShowState "Service: \"$service\"" $? || let "err=1+$err"
|
||||
done
|
||||
ShowState Services $err ${#SERVICES[@]} ';1;3'
|
||||
return $?
|
||||
}
|
||||
|
||||
function CheckDaemons {
|
||||
local err=0
|
||||
local daemon=
|
||||
local daemon_name=
|
||||
for i in ${!DAEMONS[@]}; do
|
||||
daemon="${DAEMONS[i]}"
|
||||
daemon_name="$(basename ${daemon%% *})"
|
||||
pgrep -x ${daemon_name:0:15} > /dev/null 2>/dev/null
|
||||
ShowState "Daemon: \"$daemon\"" $? || let "err=1+$err"
|
||||
done
|
||||
ShowState Daemons $err ${#DAEMONS[@]} ';1;3'
|
||||
return $?
|
||||
}
|
||||
|
||||
function CheckSystem {
|
||||
local i=
|
||||
local err=0
|
||||
local ret=0
|
||||
local output=
|
||||
local items=2
|
||||
for i in CheckServices CheckDaemons; do
|
||||
output=$($i)
|
||||
ret=$?
|
||||
if [ 254 -eq $ret ]; then
|
||||
let "items=${items}-1"
|
||||
elif [ 0 -lt $ret ]; then
|
||||
let "err=1+$err"
|
||||
fi
|
||||
if [ -z "$1" ]; then
|
||||
echo -e "$output" | tail -1
|
||||
else
|
||||
echo -e "$output"
|
||||
fi
|
||||
done
|
||||
ShowState "System \"${ROOT_DIR##*/}\"" $err $items ';1'
|
||||
exit $?
|
||||
}
|
||||
|
||||
# start here
|
||||
type pgrep > /dev/null || exit 1
|
||||
cd $(dirname $0)
|
||||
. processes || Quit 'File "processes" not found'
|
||||
[ -x rc.pwatch ] || Quit 'Executalbe process not found'
|
||||
WATCH_PROC="$(pwd)/rc.pwatch"
|
||||
cd .. && ROOT_DIR=$(pwd) || Quit 'Change dir failed'
|
||||
|
||||
case $* in
|
||||
'')
|
||||
CheckSystem
|
||||
;;
|
||||
status)
|
||||
CheckSystem detail
|
||||
;;
|
||||
start)
|
||||
for check in CheckServices CheckDaemons; do
|
||||
$check > /dev/null 2>/dev/null
|
||||
check_ret=$?
|
||||
[ 254 -eq $check_ret ] && continue
|
||||
if [ 0 -ne $check_ret ]; then
|
||||
Handle${check#Check} start
|
||||
output=$($check)
|
||||
if [ 0 -ne $? ]; then
|
||||
echo -e "$output"
|
||||
Quit "${check#Check} start failed"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
CheckSystem
|
||||
;;
|
||||
stop)
|
||||
for check in CheckDaemons CheckServices; do
|
||||
$check > /dev/null 2>/dev/null
|
||||
check_ret=$?
|
||||
[ 254 -eq $check_ret ] && continue
|
||||
[ 1 -ne $check_ret ] && Handle${check#Check} stop
|
||||
done
|
||||
CheckSystem
|
||||
;;
|
||||
startservice)
|
||||
CheckServices > /dev/null 2>/dev/null
|
||||
check_ret=$?
|
||||
if [ 254 -eq $check_ret ]; then
|
||||
CheckSystem
|
||||
elif [ 0 -ne $check_ret ]; then
|
||||
HandleServices start
|
||||
CheckSystem
|
||||
fi
|
||||
;;
|
||||
stopdaemon)
|
||||
CheckDaemons > /dev/null 2>/dev/null
|
||||
check_ret=$?
|
||||
if [ 254 -eq $check_ret ]; then
|
||||
CheckSystem
|
||||
elif [ 1 -ne $check_ret ]; then
|
||||
HandleDaemons stop
|
||||
CheckSystem
|
||||
fi
|
||||
;;
|
||||
list)
|
||||
Spreadout
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 [status|start[service]|stop[daemon]|list]"
|
||||
exit 3
|
||||
;;
|
||||
esac
|
||||
|
184
常用脚本/shell/rc.all_mul
Executable file
184
常用脚本/shell/rc.all_mul
Executable file
@ -0,0 +1,184 @@
|
||||
#!/bin/bash
|
||||
|
||||
function Quit {
|
||||
[ -n "$1" ] && echo -e "\n\033[31;1mERROR: $1\033[0m!\n"
|
||||
exit 3
|
||||
}
|
||||
|
||||
function Spreadout {
|
||||
local i=
|
||||
local service=
|
||||
local daemon=
|
||||
echo "System ${ROOT_DIR##*/} processes ..."
|
||||
for service in ${SERVICES[@]}; do
|
||||
echo "Service: \"$service\""
|
||||
done
|
||||
for i in ${!DAEMONS[@]}; do
|
||||
daemon="${DAEMONS[i]}"
|
||||
echo "Daemon: \"$daemon\""
|
||||
done
|
||||
exit 0
|
||||
}
|
||||
|
||||
function ShowState {
|
||||
local name=$1
|
||||
local err=$2
|
||||
local total=$3
|
||||
local style=$4
|
||||
if [ '0' = "$total" ]; then
|
||||
echo -e "$name \033[32${style}mnot found\033[0m ..."
|
||||
return 254
|
||||
elif [ '0' = "$err" ]; then
|
||||
echo -e "$name \033[32${style}mrunning\033[0m ..."
|
||||
return 0
|
||||
elif [ -z "$total" -o "$total" = "$err" ]; then
|
||||
echo -e "$name \033[33${style}mstopped\033[0m!"
|
||||
return 1
|
||||
else
|
||||
echo -e "$name \033[31${style}merror\033[0m!!"
|
||||
return 255
|
||||
fi
|
||||
}
|
||||
|
||||
function HandleServices {
|
||||
local service=
|
||||
for service in ${SERVICES[@]}; do
|
||||
echo "===> ${1}ing \"$service\" ..."
|
||||
systemctl $1 $service
|
||||
sleep 0.4
|
||||
done
|
||||
}
|
||||
|
||||
function HandleDaemons {
|
||||
local i=
|
||||
local daemon=
|
||||
for i in ${!DAEMONS[@]}; do
|
||||
daemon="${DAEMONS[i]}"
|
||||
echo "===> ${1}ing \"$daemon\" ..."
|
||||
$WATCH_PROC $1 $daemon
|
||||
sleep 0.4
|
||||
done
|
||||
}
|
||||
|
||||
function CheckServices {
|
||||
local err=0
|
||||
local service=
|
||||
for service in ${SERVICES[@]}; do
|
||||
systemctl status $service > /dev/null 2>/dev/null
|
||||
ShowState "Service: \"$service\"" $? || let "err=1+$err"
|
||||
done
|
||||
ShowState Services $err ${#SERVICES[@]} ';1;3'
|
||||
return $?
|
||||
}
|
||||
|
||||
function CheckDaemons {
|
||||
local err=0
|
||||
local daemon=
|
||||
local daemon_cmd=
|
||||
local output=
|
||||
local output_watch=
|
||||
for i in ${!DAEMONS[@]}; do
|
||||
daemon="${DAEMONS[i]}"
|
||||
daemon_cmd="${daemon#$(dirname ${daemon%% *})/}"
|
||||
output=$(pgrep -f "$daemon_cmd$")
|
||||
output_watch=(pgrep -f "$(basename $WATCH_PROC) +start +[^ ]*\<$daemon_cmd$")
|
||||
[ -n "$output" -a "$output" != "$output_watch" ]
|
||||
ShowState "Daemon: \"$daemon\"" $? || let "err=1+$err"
|
||||
done
|
||||
ShowState Daemons $err ${#DAEMONS[@]} ';1;3'
|
||||
return $?
|
||||
}
|
||||
|
||||
function CheckSystem {
|
||||
local i=
|
||||
local err=0
|
||||
local ret=0
|
||||
local output=
|
||||
local items=2
|
||||
for i in CheckServices CheckDaemons; do
|
||||
output=$($i)
|
||||
ret=$?
|
||||
if [ 254 -eq $ret ]; then
|
||||
let "items=${items}-1"
|
||||
elif [ 0 -lt $ret ]; then
|
||||
let "err=1+$err"
|
||||
fi
|
||||
if [ -z "$1" ]; then
|
||||
echo -e "$output" | tail -1
|
||||
else
|
||||
echo -e "$output"
|
||||
fi
|
||||
done
|
||||
ShowState "System \"${ROOT_DIR##*/}\"" $err $items ';1'
|
||||
exit $?
|
||||
}
|
||||
|
||||
# start here
|
||||
type pgrep > /dev/null || exit 1
|
||||
cd $(dirname $0)
|
||||
. processes || Quit 'File "processes" not found'
|
||||
[ -x rc.pwatch ] || Quit 'Executalbe process not found'
|
||||
WATCH_PROC="$(pwd)/rc.pwatch"
|
||||
cd .. && ROOT_DIR=$(pwd) || Quit 'Change dir failed'
|
||||
|
||||
case $* in
|
||||
'')
|
||||
CheckSystem
|
||||
;;
|
||||
status)
|
||||
CheckSystem detail
|
||||
;;
|
||||
start)
|
||||
for check in CheckServices CheckDaemons; do
|
||||
$check > /dev/null 2>/dev/null
|
||||
check_ret=$?
|
||||
[ 254 -eq $check_ret ] && continue
|
||||
if [ 0 -ne $check_ret ]; then
|
||||
Handle${check#Check} start
|
||||
output=$($check)
|
||||
if [ 0 -ne $? ]; then
|
||||
echo -e "$output"
|
||||
Quit "${check#Check} start failed"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
CheckSystem
|
||||
;;
|
||||
stop)
|
||||
for check in CheckDaemons CheckServices; do
|
||||
$check > /dev/null 2>/dev/null
|
||||
check_ret=$?
|
||||
[ 254 -eq $check_ret ] && continue
|
||||
[ 1 -ne $check_ret ] && Handle${check#Check} stop
|
||||
done
|
||||
CheckSystem
|
||||
;;
|
||||
startservice)
|
||||
CheckServices > /dev/null 2>/dev/null
|
||||
check_ret=$?
|
||||
if [ 254 -eq $check_ret ]; then
|
||||
CheckSystem
|
||||
elif [ 0 -ne $check_ret ]; then
|
||||
HandleServices start
|
||||
CheckSystem
|
||||
fi
|
||||
;;
|
||||
stopdaemon)
|
||||
CheckDaemons > /dev/null 2>/dev/null
|
||||
check_ret=$?
|
||||
if [ 254 -eq $check_ret ]; then
|
||||
CheckSystem
|
||||
elif [ 1 -ne $check_ret ]; then
|
||||
HandleDaemons stop
|
||||
CheckSystem
|
||||
fi
|
||||
;;
|
||||
list)
|
||||
Spreadout
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 [status|start[service]|stop[daemon]|list]"
|
||||
exit 3
|
||||
;;
|
||||
esac
|
||||
|
50
常用脚本/shell/rc.pwatch
Executable file
50
常用脚本/shell/rc.pwatch
Executable file
@ -0,0 +1,50 @@
|
||||
#!/bin/bash
|
||||
|
||||
type pgrep > /dev/null || exit 1
|
||||
[ 2 -gt $# ] && echo -e "\tUsage:$0 {start|stop} {cmd}\n" && exit 1
|
||||
|
||||
cd $(dirname $0)/../
|
||||
WATCH_LOG=/var/log/watch_proc.log
|
||||
WATCH_NAME=$(basename $0)
|
||||
OPERATION=$1
|
||||
shift
|
||||
PROC_CMD="$*"
|
||||
PROC_NAME=$(basename ${PROC_CMD%% *})
|
||||
|
||||
function StopProc {
|
||||
local cpids=$(pgrep -P $1)
|
||||
local cpid=
|
||||
printf "%${2}s|-$1\n"
|
||||
[ -f /proc/$1/stat ] && kill $1
|
||||
local n=$(expr 2 + $2)
|
||||
for cpid in $cpids; do
|
||||
StopProc "$cpid" $n
|
||||
done
|
||||
}
|
||||
|
||||
case $OPERATION in
|
||||
start)
|
||||
pid=$(pgrep -f "$WATCH_NAME +start +[^ ]*\<$PROC_NAME\>")
|
||||
pid=$(echo -e "$pid"|grep -v "^$$$")
|
||||
[ -n "$pid" ] && echo "WATCH on \"$PROC_CMD\" is runing with PID:$pid" && exit 2
|
||||
while :; do
|
||||
$PROC_CMD > /dev/null 2>/dev/null &
|
||||
pid=$!
|
||||
while sleep 4; do [ -f /proc/$pid/stat ] || break; done
|
||||
echo "$(date +'%F %T') \"$PROC_CMD\" quit exceptionally!" >> $WATCH_LOG
|
||||
StopProc "$pid" 0 >> $WATCH_LOG
|
||||
echo "$(date +'%F %T') \"$PROC_CMD\" restart." >> $WATCH_LOG
|
||||
done > /dev/null 2>/dev/null & ;;
|
||||
stop)
|
||||
ppid=$(pgrep -f "$WATCH_NAME +start +[^ ]*\<$PROC_NAME\>")
|
||||
[ -z "$ppid" ] && echo "No WATCH on \"$PROC_CMD\" !" && exit 1
|
||||
pid=$(pgrep -lP $ppid|grep -v '^[0-9]\+ \+sleep$'|awk '{print $1}')
|
||||
[ -z "$pid" ] && echo "No process: \"$PROC_CMD\"" && kill -9 $ppid && exit 0
|
||||
kill -9 $ppid
|
||||
echo "$(date +'%F %T') \"$PROC_CMD\" quit manually." >> $WATCH_LOG
|
||||
StopProc "$pid" 0
|
||||
exit 0 ;;
|
||||
*)
|
||||
echo -e "\tUsage:$0 start|stop cmd argv ...\n" ;;
|
||||
esac
|
||||
|
51
常用脚本/shell/rc.pwatch_mul
Executable file
51
常用脚本/shell/rc.pwatch_mul
Executable file
@ -0,0 +1,51 @@
|
||||
#!/bin/bash
|
||||
|
||||
type pgrep > /dev/null || exit 1
|
||||
[ 2 -gt $# ] && echo -e "\tUsage:$0 {start|stop} {cmd}\n" && exit 1
|
||||
|
||||
cd $(dirname $0)/../
|
||||
WATCH_LOG=/var/log/watch_proc.log
|
||||
WATCH_NAME=$(basename $0)
|
||||
OPERATION=$1
|
||||
shift
|
||||
PROC_CMD="$*"
|
||||
PROC_NAME=$(basename ${PROC_CMD%% *})
|
||||
PROC_ARGS="${PROC_CMD#*$PROC_NAME}"
|
||||
|
||||
function StopProc {
|
||||
local cpids=$(pgrep -P $1)
|
||||
local cpid=
|
||||
printf "%${2}s|-$1\n"
|
||||
[ -f /proc/$1/stat ] && kill $1
|
||||
local n=$(expr 2 + $2)
|
||||
for cpid in $cpids; do
|
||||
StopProc "$cpid" $n
|
||||
done
|
||||
}
|
||||
|
||||
case $OPERATION in
|
||||
start)
|
||||
pid=$(pgrep -f "$WATCH_NAME +start +[^ ]*\<$PROC_NAME\>$PROC_ARGS$")
|
||||
pid=$(echo -e "$pid"|grep -v "^$$$")
|
||||
[ -n "$pid" ] && echo "WATCH on \"$PROC_CMD\" is runing with PID:$pid" && exit 2
|
||||
while :; do
|
||||
$PROC_CMD > /dev/null 2>/dev/null &
|
||||
pid=$!
|
||||
while sleep 4; do [ -f /proc/$pid/stat ] || break; done
|
||||
echo "$(date +'%F %T') \"$PROC_CMD\" quit exceptionally!" >> $WATCH_LOG
|
||||
StopProc "$pid" 0 >> $WATCH_LOG
|
||||
echo "$(date +'%F %T') \"$PROC_CMD\" restart." >> $WATCH_LOG
|
||||
done > /dev/null 2>/dev/null & ;;
|
||||
stop)
|
||||
ppid=$(pgrep -f "$WATCH_NAME +start +[^ ]*\<$PROC_NAME\>$PROC_ARGS$")
|
||||
[ -z "$ppid" ] && echo "No WATCH on \"$PROC_CMD\" !" && exit 1
|
||||
pid=$(pgrep -lP $ppid|grep -v '^[0-9]\+ \+sleep$'|awk '{print $1}')
|
||||
[ -z "$pid" ] && echo "No process: \"$PROC_CMD\"" && kill -9 $ppid && exit 0
|
||||
kill -9 $ppid
|
||||
echo "$(date +'%F %T') \"$PROC_CMD\" quit manually." >> $WATCH_LOG
|
||||
StopProc "$pid" 0
|
||||
exit 0 ;;
|
||||
*)
|
||||
echo -e "\tUsage:$0 start|stop cmd argv ...\n" ;;
|
||||
esac
|
||||
|
351
常用脚本/transfer
Executable file
351
常用脚本/transfer
Executable file
@ -0,0 +1,351 @@
|
||||
#!/usr/bin/python2
|
||||
# -*- coding:utf-8 -*-
|
||||
#=========================================
|
||||
# Filename : transfer.py
|
||||
# Author : Colben
|
||||
# Create : 2017-05-11 12:27
|
||||
#=========================================
|
||||
|
||||
import os, sys, platform
|
||||
import shutil
|
||||
import re
|
||||
import time
|
||||
import zipfile
|
||||
import BaseHTTPServer, HTMLParser
|
||||
import requests, mimetypes, base64
|
||||
from SocketServer import ThreadingMixIn
|
||||
from threading import Thread
|
||||
|
||||
reload(sys)
|
||||
sys.setdefaultencoding("utf-8")
|
||||
|
||||
try:
|
||||
from cStringIO import StringIO
|
||||
except ImportError:
|
||||
from StringIO import StringIO
|
||||
|
||||
|
||||
KEY = base64.b64encode('lijc:lijc')
|
||||
AUTH = ('lijc', 'lijc')
|
||||
URLS = []
|
||||
THREAD_RESULT = {}
|
||||
|
||||
def sizeof_fmt(num):# <<FOLD<<
|
||||
for x in ['B', 'KB', 'MB', 'GB']:
|
||||
if num < 1024.0:
|
||||
return "%3.1f%s" % (num, x)
|
||||
num /= 1024.0
|
||||
return "%3.1f%s" % (num, 'TB')# >>FOLD>>
|
||||
|
||||
def modification_date(filename):
|
||||
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getmtime(filename)))
|
||||
|
||||
def handle_attachment(filename, user_agent):# <<FOLD<<
|
||||
r, info = True, "File '%s' upload success!"
|
||||
if filename.endswith('.zip'):
|
||||
try:
|
||||
zf = zipfile.ZipFile(filename, 'r')
|
||||
zf.extractall('/home/attachment/')
|
||||
except Exception, e:
|
||||
r, info = False, str(e)
|
||||
else:
|
||||
try:
|
||||
dest_file = os.path.join('/home/attachment/', os.path.basename(filename))
|
||||
if os.path.exists(dest_file):
|
||||
os.remove(dest_file)
|
||||
os.rename(filename, dest_file)
|
||||
filename = dest_file
|
||||
except Exception, e:
|
||||
r, info = False, str(e)
|
||||
if r and user_agent != 'upload/sync':
|
||||
for url in URLS:
|
||||
sub_thread = Thread(target=sync_file, args=(url, filename,))
|
||||
sub_thread.setDaemon(1)
|
||||
sub_thread.start()
|
||||
return (r, info%os.path.basename(filename))# >>FOLD>>
|
||||
|
||||
def sync_file(url, filename):# <<FOLD<<
|
||||
THREAD_RESULT[url] = '<br>Syncing to %s ...'%url
|
||||
headers = {'User-Agent': 'upload/sync'}
|
||||
files = {'file': (os.path.basename(filename), open(filename, 'rb'), 'upload/file')}
|
||||
resp = requests.post(url, auth=AUTH, headers=headers, files=files)
|
||||
THREAD_RESULT[url] = "<br>Sync to '%s': %s"%(url, resp.headers['Sync-Result'])
|
||||
return# >>FOLD>>
|
||||
|
||||
class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):# <<FOLD<<
|
||||
parser=HTMLParser.HTMLParser()
|
||||
|
||||
#def do_GET(self):# <<FOLD<<
|
||||
# """Serve a GET request."""
|
||||
# f = self.send_head()
|
||||
# if f:
|
||||
# self.copyfile(f, self.wfile)
|
||||
# f.close()# >>FOLD>>
|
||||
|
||||
def do_GET(self):# <<FOLD<<
|
||||
''' Present frontpage with user authentication. '''
|
||||
if self.headers.getheader('Authorization') == None:
|
||||
self.do_AUTHHEAD()
|
||||
self.wfile.write('no auth header received')
|
||||
pass
|
||||
elif self.headers.getheader('Authorization') == 'Basic '+KEY:
|
||||
"""Serve a GET request."""
|
||||
f = self.send_head()
|
||||
if f:
|
||||
self.copyfile(f, self.wfile)
|
||||
f.close()
|
||||
pass
|
||||
else:
|
||||
self.do_AUTHHEAD()
|
||||
self.wfile.write(self.headers.getheader('Authorization'))
|
||||
self.wfile.write('not authenticated')
|
||||
pass# >>FOLD>>
|
||||
|
||||
def do_HEAD(self):# <<FOLD<<
|
||||
"""Serve a HEAD request."""
|
||||
f = self.send_head()
|
||||
if f:
|
||||
f.close()# >>FOLD>>
|
||||
|
||||
def do_AUTHHEAD(self):# <<FOLD<<
|
||||
print "send header"
|
||||
self.send_response(401)
|
||||
self.send_header('WWW-Authenticate', 'Basic realm=\"Test\"')
|
||||
self.send_header('Content-type', 'text/html')
|
||||
self.end_headers()# >>FOLD>>
|
||||
|
||||
def do_POST(self):# <<FOLD<<
|
||||
"""Serve a POST request."""
|
||||
r, info = self.deal_post_data()
|
||||
f = StringIO()
|
||||
f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
|
||||
f.write('<html>\n<title>Upload Result</title>\n')
|
||||
f.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\n')
|
||||
f.write('<body>\n<h2>Upload Result</h2>\n')
|
||||
f.write('\n<br><a href="../">返回上级</a>  <a href="/">返回首页</a>')
|
||||
f.write('<hr>\n')
|
||||
if r:
|
||||
f.write('<strong>Success:</strong>')
|
||||
else:
|
||||
f.write('<strong>Failed:</strong>')
|
||||
f.write(info)
|
||||
self.send_footer(f)
|
||||
length = f.tell()
|
||||
f.seek(0)
|
||||
self.send_response(200)
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.send_header("Content-Length", str(length))
|
||||
if self.headers['user-agent'] == 'upload/sync':
|
||||
self.send_header("Sync-Result", info)
|
||||
self.end_headers()
|
||||
if f:
|
||||
self.copyfile(f, self.wfile)
|
||||
f.close()# >>FOLD>>
|
||||
|
||||
def deal_post_data(self):# <<FOLD<<
|
||||
boundary = self.headers.plisttext.split("=")[1]
|
||||
remainbytes = int(self.headers['content-length'])
|
||||
line = self.rfile.readline()
|
||||
remainbytes -= len(line)
|
||||
if not boundary in line:
|
||||
return (False, "Content NOT begin with boundary")
|
||||
line = self.rfile.readline()
|
||||
remainbytes -= len(line)
|
||||
fn = re.findall(r'Content-Disposition.*name="file"; filename="(.*)"', line)
|
||||
if not fn:
|
||||
return (False, "Can't find out file name...")
|
||||
path = self.translate_path(self.path)
|
||||
osType = platform.system()
|
||||
try:
|
||||
if osType != "Linux":
|
||||
fn = os.path.join(path, fn[0].decode('gbk').encode('utf-8'))
|
||||
else:
|
||||
fn = os.path.join(path, fn[0])
|
||||
except Exception, e:
|
||||
return (False, "文件名请不要用中文,或者使用IE上传中文名的文件。")
|
||||
fn_utf8 = self.parser.unescape(fn)
|
||||
if os.path.exists(fn_utf8):
|
||||
os.remove(fn_utf8)
|
||||
line = self.rfile.readline()
|
||||
remainbytes -= len(line)
|
||||
line = self.rfile.readline()
|
||||
remainbytes -= len(line)
|
||||
try:
|
||||
out = open(fn_utf8, 'wb')
|
||||
except IOError:
|
||||
return (False, "Can't create file to write, do you have permission to write?")
|
||||
|
||||
preline = self.rfile.readline()
|
||||
remainbytes -= len(preline)
|
||||
while remainbytes > 0:
|
||||
line = self.rfile.readline()
|
||||
remainbytes -= len(line)
|
||||
if boundary in line:
|
||||
preline = preline[0:-1]
|
||||
if preline.endswith('\r'):
|
||||
preline = preline[0:-1]
|
||||
out.write(preline)
|
||||
out.close()
|
||||
if os.path.dirname(fn).endswith('/attachment'):
|
||||
return handle_attachment(fn, self.headers['user-agent'])
|
||||
else:
|
||||
return (True, "File '%s' upload success!" % os.path.basename(fn))
|
||||
else:
|
||||
out.write(preline)
|
||||
preline = line
|
||||
return (False, "Unexpect Ends of data.")# >>FOLD>>
|
||||
|
||||
def send_head(self):# <<FOLD<<
|
||||
path = self.translate_path(self.path)
|
||||
f = None
|
||||
if os.path.isdir(path):
|
||||
if not self.path.endswith('/'):
|
||||
# redirect browser - doing basically what apache does
|
||||
self.send_response(301)
|
||||
self.send_header("Location", self.path + "/")
|
||||
self.end_headers()
|
||||
return None
|
||||
for index in "index.html", "index.htm":
|
||||
index = os.path.join(path, index)
|
||||
if os.path.exists(index):
|
||||
path = index
|
||||
break
|
||||
else:
|
||||
return self.list_directory(path)
|
||||
ctype = self.guess_type(path)
|
||||
try:
|
||||
# Always read in binary mode. Opening files in text mode may cause
|
||||
# newline translations, making the actual size of the content
|
||||
# transmitted *less* than the content-length!
|
||||
f = open(path, 'rb')
|
||||
except IOError:
|
||||
self.send_error(404, "File not found")
|
||||
return None
|
||||
self.send_response(200)
|
||||
self.send_header("Content-type", ctype)
|
||||
fs = os.fstat(f.fileno())
|
||||
self.send_header("Content-Length", str(fs[6]))
|
||||
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
|
||||
self.end_headers()
|
||||
return f# >>FOLD>>
|
||||
|
||||
def list_directory(self, path):# <<FOLD<<
|
||||
try:
|
||||
list = filter(lambda x: not x.startswith('.'), os.listdir(path))
|
||||
except os.error:
|
||||
self.send_error(404, "No permission to list directory")
|
||||
return None
|
||||
list.sort(key=lambda a: a.lower())
|
||||
f = StringIO()
|
||||
displaypath = requests.utils.unquote(self.path)
|
||||
#displaypath = requests.utils.cgi.escape(requests.utils.unquote(self.path))
|
||||
f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
|
||||
f.write('<html>\n<head><title>Directory %s</title>\n' % displaypath)
|
||||
f.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\n')
|
||||
f.write('<style type="text/css">a:link,a:visited{text-decoration:none;}a:hover{text-decoration:underline;}</style>')
|
||||
f.write('\n</head>\n<body>\n<h2>当前位置: %s</h2>\n' % displaypath)
|
||||
if '/' != self.path:
|
||||
f.write('\n<a href="../">返回上级</a>  <a href="/">返回首页</a>')
|
||||
f.write('<hr>\n<form ENCTYPE="multipart/form-data" method="post">')
|
||||
f.write('<input name="file" type="file"/>')
|
||||
f.write('<input type="submit" value="上传"/>')
|
||||
f.write('\n</form>\n<hr>\n<table>\n')
|
||||
if not list:
|
||||
f.write('空目录\n')
|
||||
else:
|
||||
for name in list:
|
||||
fullname = os.path.join(path, name)
|
||||
colorName = displayname = linkname = name
|
||||
# Append / for directories or @ for symbolic links
|
||||
if os.path.isdir(fullname):
|
||||
#colorName = '<span style="color: #0066CC;">' + name + '/</span>'
|
||||
colorName = name + '/'
|
||||
displayname = name
|
||||
linkname = name + "/"
|
||||
if os.path.isfile(fullname):
|
||||
colorName = '<span style="color: #9F5000;">' + name + '</span>'
|
||||
displayname = name
|
||||
if os.path.islink(fullname):
|
||||
colorName = '<span style="color: #00CACA;">' + name + '@</span>'
|
||||
displayname = name
|
||||
# Note: a link to a directory displays with @ and links with /
|
||||
filename = os.getcwd() + '/' + displaypath + displayname
|
||||
f.write(
|
||||
'<tr><td width="66%%"><a href="%s">%s</a></td><td width="9%%">%s</td><td width="25%%">%s</td></tr>\n'
|
||||
% (requests.utils.quote(linkname), colorName,
|
||||
sizeof_fmt(os.path.getsize(filename)), modification_date(filename)))
|
||||
f.write('</table>')
|
||||
self.send_footer(f)
|
||||
length = f.tell()
|
||||
f.seek(0)
|
||||
self.send_response(200)
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.send_header("Content-Length", str(length))
|
||||
self.end_headers()
|
||||
return f# >>FOLD>>
|
||||
|
||||
def send_footer(self, f):# <<FOLD<<
|
||||
f.write('\n<hr><b>同步状态:</b>')
|
||||
for sync_result in THREAD_RESULT.values():
|
||||
f.write(sync_result)
|
||||
f.write('\n<hr>\n<b>注意事项:</b>\n<ul>\n')
|
||||
f.write('<li>上传同名文件时,会覆盖已存在的旧文件</li>\n')
|
||||
f.write('</ul>')
|
||||
f.write('\n<hr>\n<b>站外链接:</b>\n<ul>\n')
|
||||
f.write('<li><a href="https://www.baidu.com/">百度</a></li>\n')
|
||||
f.write('</ul><hr>\n</body>\n</html>\n')# >>FOLD>>
|
||||
|
||||
def translate_path(self, path):# <<FOLD<<
|
||||
# abandon query parameters
|
||||
path = path.split('?', 1)[0]
|
||||
path = path.split('#', 1)[0]
|
||||
path = os.path.normpath(requests.utils.unquote(path))
|
||||
words = path.split('/')
|
||||
words = filter(None, words)
|
||||
path = os.getcwd()
|
||||
for word in words:
|
||||
drive, word = os.path.splitdrive(word)
|
||||
head, word = os.path.split(word)
|
||||
if word in (os.curdir, os.pardir): continue
|
||||
path = os.path.join(path, word)
|
||||
return path# >>FOLD>>
|
||||
|
||||
def copyfile(self, source, outputfile):
|
||||
shutil.copyfileobj(source, outputfile)
|
||||
|
||||
def guess_type(self, path):# <<FOLD<<
|
||||
base, ext = os.path.splitext(path)
|
||||
if ext in self.extensions_map:
|
||||
return self.extensions_map[ext]
|
||||
ext = ext.lower()
|
||||
if ext in self.extensions_map:
|
||||
return self.extensions_map[ext]
|
||||
else:
|
||||
return self.extensions_map['']# >>FOLD>>
|
||||
|
||||
if not mimetypes.inited:
|
||||
mimetypes.init() # try to read system mime.types
|
||||
extensions_map = mimetypes.types_map.copy()
|
||||
extensions_map.update({
|
||||
'': 'application/octet-stream', # Default
|
||||
'.py': 'text/plain',
|
||||
'.c': 'text/plain',
|
||||
'.h': 'text/plain',
|
||||
})# >>FOLD>>
|
||||
|
||||
|
||||
class ThreadingServer(ThreadingMixIn, BaseHTTPServer.HTTPServer):
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# test()
|
||||
|
||||
# 单线程
|
||||
# srvr = BaseHTTPServer.HTTPServer(('', 12306), SimpleHTTPRequestHandler)
|
||||
|
||||
# 多线程
|
||||
srvr = ThreadingServer(('', 12306), SimpleHTTPRequestHandler)
|
||||
print 'Listening ', srvr.server_address ,'...'
|
||||
srvr.serve_forever()
|
||||
|
Loading…
x
Reference in New Issue
Block a user