-
Notifications
You must be signed in to change notification settings - Fork 1
/
daemon.xml
678 lines (614 loc) · 20.9 KB
/
daemon.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V5.0//EN"
"http://www.oasis-open.org/docbook/xml/5.0b5/dtd/docbook.dtd" [
<!ENTITY article.author.xml SYSTEM "../common/article.author.xml">
<!ENTITY book.info.legalnotice.xml SYSTEM "../common/book.info.legalnotice.xml">
<!ENTITY book.info.abstract.xml SYSTEM "../common/book.info.abstract.xml">
]>
<article xml:base="http://netkiller.sourceforge.net/article/"
xmlns="http://docbook.org/ns/docbook" xml:lang="zh-cn">
<articleinfo>
<title>Linux 应用程序开发入门</title>
<subtitle>http://netkiller.github.io/journal/daemon.html</subtitle>
&article.author.xml;
<pubdate>$Date: 2012-02-01 14:03:53 +0800 (Wed, 01 Feb 2012) $</pubdate>
<!--
<editor>
<firstname>Tabatha</firstname>
<surname>Marshall</surname>
<contrib>Language review of version 0.8</contrib>
</editor>
<othercredit role='converter'>
<firstname>David</firstname>
<surname>Merrill</surname>
<contrib>Conversion from HTML to DocBook v3.1 (SGML).</contrib>
</othercredit>
<revhistory>
<revision>
<revnumber>1.0</revnumber>
<date>YYYY-MM-DD</date>
<authorinitials>ABC</authorinitials>
</revremark>first official release</revremark>
</revision>
<revision>
<revnumber>0.9</revnumber>
<date>YYYY-MM-DD</date>
<authorinitials>ABC</authorinitials>
<revremark>First draft</revremark>
</revision>
</revhistory>
-->
&book.info.legalnotice.xml;
<abstract>
<para>我会实现一个守护进程,从这个程序你将了解,Linux 应用程序开发基本流程</para>
<para>我们将实现一个远程shell的功能,可以通过tcp协议,运行远程机器上的命令或shell脚本</para>
<para>通过这个命令可以实现批量操作,管理上千台服务器。需要发挥你的想象力,灵活使用它。</para>
<para>写这个脚本,我是为了替代SSH远程操作,因为SSH不能控制运行命令,操作风险大,也不安全。</para>
<para>程序还不完善,还需要很多后续改进工作,比如通过SSL建立Socket链接,用户认证,ACL访问控制等等.</para>
</abstract>
&book.info.abstract.xml;
<keywordset>
<keyword>python, os.fork, getopt.getopt, logging</keyword>
<keyword>init.d, rc.d, inittab</keyword>
<keyword>nodekeeper</keyword>
</keywordset>
</articleinfo>
<section>
<title>环境</title>
<para>OS: Ubuntu 11.10</para>
<para>Python: 3.2.2</para>
<para>程序目录: /srv/nodekeeper</para>
<para>目录与相关文件</para>
<screen>
$ cd /srv
$ find nodekeeper | grep -v .svn
nodekeeper
nodekeeper/nodekeeper.ubuntu
nodekeeper/nodekeeper.cenos
nodekeeper/etc
nodekeeper/etc/commands.cfg
nodekeeper/etc/protocol.cfg
nodekeeper/bin
nodekeeper/bin/nodekeeper
nodekeeper/bin/console
</screen>
</section>
<section>
<title>nodekeeper 主程序</title>
<screen>
<![CDATA[
$ cat nodekeeper/bin/nodekeeper
#!/usr/bin/env python3
#/bin/env python3
#-*- coding: utf-8 -*-
##############################################
# Home : http://netkiller.sf.net
# Author: Neo <[email protected]>
##############################################
import asyncore, asynchat, socket, threading
import subprocess, os, sys, getopt, configparser, logging
import string, re
from multiprocessing import Process
class Backend(asyncore.dispatcher):
queue = []
def __init__(self, host, port,config):
asyncore.dispatcher.__init__(self)
self.host = host
self.port = port
self.config = config
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.bind((host,port))
self.listen(10)
try:
cfg = Protocol(config['protocol'], self.host)
#self.protocols = cfg.items(self.host)
self.protocols = cfg.all()
self.sections = cfg.sections()
except configparser.NoSectionError as err:
print("Error: %s %s" %(err, config['protocol']))
sys.exit(2)
try:
logging.basicConfig(level=logging.NOTSET,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filename=config['logfile'],
filemode='a')
self.logging = logging.getLogger()
#self.logging.debug('Test')
except AttributeError as err:
print("Error: %s %s" %(err, config['logfile']))
sys.exit(2)
def handle_accept (self):
conn, addr = self.accept()
self.queue.append(addr)
request_handler(conn, self)
def handle_connect(self):
pass
def handle_expt(self):
self.close()
def handle_close(self):
self.close()
class request_handler(asynchat.async_chat):
def __init__(self, sock, resource):
asynchat.async_chat.__init__(self, sock=sock)
self.sessions = resource
self.buffer = b''
self.set_terminator(b"\r\n")
self.logging = resource.logging
self.protocols = resource.protocols
self.sections = resource.sections
self.host = self.sessions.host
def handle_connect(self):
# connection succeeded
#self.logging.info('')
pass
def handle_expt(self):
# connection failed
self.close()
def collect_incoming_data(self, data):
"""Buffer the data"""
#self.buffer.append(data)
self.buffer = data
def found_terminator(self):
try:
buffer = bytes.decode(self.buffer)
except UnicodeDecodeError:
print("\r\nError: ",err)
buffer = ''
try:
execute = re.split(' ', buffer)
command = execute[0]
parameter = ' '.join( execute[1:])
response = b''
screen = ''
if self.buffer == b'quit' or self.buffer == b'exit' :
self.push(b'shutdown!!!\r\n')
self.close_when_done()
elif self.buffer == b'help' or self.buffer == b'?':
screen = "Help may be requested at any point in a command by entering a question mark '?' or 'help'. the help list will be showing the available options.\r\n"
for cmd,v in self.protocols :
screen += cmd + "\r\n"
elif self.buffer == b'sections' :
for sect in self.sections :
screen += sect + "\r\n"
elif self.buffer == b'help.html' :
for cmd,v in self.protocols :
screen += '<a href="?host='+self.host+'&cmd='+cmd+'">'+ cmd +'</a><br />' + "\r\n"
elif self.buffer == b'enable':
self.prompt = b'#'
elif self.buffer == b'end' or self.buffer == b'^z':
self.prompt = b'>'
else:
proto = dict(self.protocols)
if command in proto :
run = proto[command] + ' ' + parameter
screen = subprocess.getoutput(run)
if screen :
response = bytes(screen + "\r\n",'utf8')
self.push(response)
self.logging.info(bytes.decode(self.buffer))
self.buffer = b''
self.close_when_done()
except :
self.close_when_done()
sys.exit(2)
class Protocol():
config = None
agreement = None
def __init__(self,cfg = 'protocol.cfg',sections = ''):
self.config = configparser.SafeConfigParser()
self.config.read(cfg)
#self.agreement = self.config.items('common')
def sections(self):
return self.config.sections()
def items(self, sections):
self.agreement = self.config.items(sections)
return self.agreement
def dicts(self):
return dict(self.agreement)
def all(self):
self.agreement = []
for section in self.config.sections():
self.agreement += self.config.items(section)
return self.agreement
def main():
daemon = False
host = 'localhost'
port = 7800
pidfile = ''
logfile = ''
cfgfile = ''
try:
opts, args = getopt.getopt(sys.argv[1:], "h:p:d?v", [ "daemon","host=","port=", 'help',"h=","p=", "basedir=", "pidfile=", "config=", "protocol=", "logfile="])
if not opts :
usage()
sys.exit()
for o, a in opts :
if o in ('-?', '--help') :
usage()
sys.exit()
elif o in ("-v", "--verbose"):
usage()
sys.exit()
elif o in ("-d", "--daemon"):
daemon = True
elif o in ("-h", "--host"):
host = a
elif o in ("-p", "--port"):
port = int(a)
elif o in ("--basedir"):
BASEDIR = a
elif o in ("--pidfile"):
pidfile = a
elif o in ("--config"):
cfgfile = a
elif o in ("--protocol"):
protocol = a
elif o in ("--logfile"):
logfile = a
else:
assert False, "unhandled option"
except getopt.GetoptError as err:
# print help information and exit:
usage()
sys.exit(2)
try:
if daemon :
pid = os.fork()
if pid > 0:
#exit first parent
sys.exit(0)
myself = str(sys.argv[0].split('/')[-1:][0])
#pidfile = os.getpid()
if not pidfile :
pidfile = '/var/run/'+myself+'.pid'
file = open(pidfile,'w')
file.write(str(os.getpid()))
file.close()
if not cfgfile :
cfgfile = ''+myself+'.cfg'
if not logfile :
logfile = '/var/log/'+myself+'.log'
config = dict({'cfgfile':cfgfile, 'pidfile':pidfile, 'logfile':logfile, 'protocol':protocol})
Backend(host,port,config)
asyncore.loop(timeout=30, use_poll=True)
except socket.error as err:
print("\r\nError: ",err)
sys.exit(2)
except IOError as err:
print("\r\nError: ",err)
sys.exit(2)
def usage():
myself = str(sys.argv[0].split('/')[-1:][0])
print("Usage: %s -d -h <ip address> -p <7800>" % myself );
print("Development and deployment administration platform")
print("\r\nMandatory arguments to long options are mandatory for short options too.")
print("\t-?, --help")
print("\t-v, --verbose")
print("\t-d, --daemon")
print("\t-h, --host \t\t(default localhost)")
print("\t-p, --port")
print("\t --config \t\t(default %s.cfg)" % myself)
print("\t --protocol \t\t(default %s.cfg)" % "protocol.cfg")
print("\t --pidfile \t\t(default /var/run/%s.pid)" % myself)
print("\t --logfile \t\t(default /var/log/%s.log)" % myself)
print("\r\nExample:")
print("\t%s --daemon --host localhost --port 7800" % myself)
print("\t%s -d -h localhost -p 7800" % myself)
print("\r\nSee http://netkiller.sf.net/ for updates, bug reports, and answers, \r\nif you have no web access, by sending email to Neo Chan<[email protected]>. ")
# Exit status is 0 if OK, 1 if minor problems, 2 if serious trouble.
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print ("Crtl+C Pressed. Shutting down.")
]]>
</screen>
<section>
<title>帮助信息</title>
<para>usage()</para>
<screen>
<![CDATA[
Usage: nodekeeper -d -h <ip address> -p <7800>
Development and deployment administration platform
Mandatory arguments to long options are mandatory for short options too.
-?, --help
-v, --verbose
-d, --daemon
-h, --host (default localhost)
-p, --port
--config (default nodekeeper.cfg)
--protocol (default protocol.cfg.cfg)
--pidfile (default /var/run/nodekeeper.pid)
--logfile (default /var/log/nodekeeper.log)
Example:
nodekeeper --daemon --host localhost --port 7800
nodekeeper -d -h localhost -p 7800
See http://netkiller.sf.net/ for updates, bug reports, and answers,
if you have no web access, by sending email to Neo Chan<[email protected]>.
]]>
</screen>
</section>
<section>
<title>参数处理</title>
<para>getopt.getopt 实现Unix风格的命令参数,例如:</para>
<screen>
nodekeeper --daemon --host localhost --port 7800
--host localhost --port 7800 IP地址与端口参数
--daemon 参数实现后台运行
</screen>
<para>具体实现代码</para>
<screen>
<![CDATA[
try:
opts, args = getopt.getopt(sys.argv[1:], "h:p:d?v", [ "daemon","host=","port=", 'help',"h=","p=", "basedir=", "pidfile=", "config=", "protocol=", "logfile="])
if not opts :
usage()
sys.exit()
for o, a in opts :
if o in ('-?', '--help') :
usage()
sys.exit()
elif o in ("-v", "--verbose"):
usage()
sys.exit()
elif o in ("-d", "--daemon"):
daemon = True
elif o in ("-h", "--host"):
host = a
elif o in ("-p", "--port"):
port = int(a)
elif o in ("--basedir"):
BASEDIR = a
elif o in ("--pidfile"):
pidfile = a
elif o in ("--config"):
cfgfile = a
elif o in ("--protocol"):
protocol = a
elif o in ("--logfile"):
logfile = a
else:
assert False, "unhandled option"
except getopt.GetoptError as err:
# print help information and exit:
usage()
sys.exit(2)
]]>
</screen>
</section>
<section>
<title>后台运行</title>
<para>--daemon 参数实现后台运行,原理是首先通过os.fork()克隆一个进程,然后退出当前进程,克隆的新进程继续运行</para>
<para>如果是Shell程序,你可使用“&”符号后台运行,但作为一个应用程序,使用“&”显得不专业。</para>
<para>具体实现的代码如下</para>
<screen>
<![CDATA[
if daemon :
pid = os.fork()
if pid > 0:
#exit first parent
sys.exit(0)
]]>
</screen>
<para>程序一旦进入后台,当前进程即将关闭,所以你必须保存PID,为后面的推出程序操作使用,这里我们可以通过 --pidfile 指定一个pid文件</para>
</section>
<section>
<title>日志记录</title>
<para>程序一旦进入后台,你只能通过ps,pstree, top 等命令查看状态,运行情况必须通过日志的形式,打印出来</para>
<para>具体实现代码如下:</para>
<screen>
<![CDATA[
logging.basicConfig(level=logging.NOTSET,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filename=config['logfile'],
filemode='a')
self.logging = logging.getLogger()
self.logging.debug('Test')
]]>
</screen>
</section>
<section>
<title>多线程</title>
<para>继承 asynchat.async_chat 实现多线程</para>
<screen>
<![CDATA[
class request_handler(asynchat.async_chat):
def __init__(self, sock, resource):
asynchat.async_chat.__init__(self, sock=sock)
]]>
</screen>
<para>连接数限制</para>
<screen>
self.listen(10)
</screen>
<para>可以将这个参数提出来,然后通过命令行设置。</para>
<screen>
nodekeeper --daemon --maxconn 100 --host localhost --port 7800
self.max_connect = maxconn
self.listen(self.max_connect)
</screen>
</section>
</section>
<section>
<title>配置文件</title>
<screen>
<![CDATA[
$ cat nodekeeper/etc/protocol.cfg
[system]
ls = ls
os.hosts = cat /etc/hosts
os.issue = cat /etc/issue
os.memory = free
os.who = who
os.harddisk = df -h
os.uptime = uptime
os.cpuinfo = cat /proc/cpuinfo
os.meminfo = cat /proc/meminfo
os.dmesg = dmesg
os.process = ps aux
os.summary = echo
network.status = netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
network.netstat = netstat -nlp
network.ifconfig = ifconfig
network.route = ip route
[apache]
apache.start = /usr/local/apache/bin/apachectl start
apache.stop = /usr/local/apache/bin/apachectl stop
apache.restart = /usr/local/apache/bin/apachectl restart
apache.status = ps ax |grep httpd
apache.conf = cat /usr/local/apache/conf/httpd.conf
apache.conf.vhost = cat /usr/local/apache/conf/extra/httpd-vhosts.conf
apache.logs.now =
apache.logs.tail =
[resin]
resin.start = /usr/local/resin/bin/httpd.sh start
resin.stop = /usr/local/resin/bin/httpd.sh stop
resin.restart = /usr/local/resin/bin/httpd.sh restart
resin.status = /usr/local/resin/bin/httpd.sh status
resin.conf = cat /usr/local/resin/conf/resin.conf
[www]
www.list = ls -1 /www
www.permission = find /www -type d -exec chmod 755 {} \; find /www -type f -exec chmod 644 {} \;
www.permission.777 = chmod 777 -R /www/*
lamp.status = ps ax |grep -E "mysqld|httpd|resin"
[samba]
samba.start = /etc/init.d/smb start
samba.stop = /etc/init.d/smb stop
samba.restart = /etc/init.d/smb restart
samba.status = /etc/init.d/smb status
[mysql]
mysql.start = /etc/init.d/mysql start
mysql.stop = /etc/init.d/mysql stop
mysql.restart = /etc/init.d/mysql restart
[memcache]
memcache.start = /etc/init.d/memcache start
memcache.stop = /etc/init.d/memcache stop
memcache.restart = /etc/init.d/memcache restart
[vsftpd]
vsftpd.start = /etc/init.d/vsftpd start
vsftpd.stop = /etc/init.d/vsftpd stop
vsftpd.restart = /etc/init.d/vsftpd restart
vsftpd.status = /etc/init.d/vsftpd status
]]>
</screen>
</section>
<section>
<title>init.d 脚本</title>
<para>Linux 所有守护进程都是用init.d下面的脚本来管理</para>
<para>当人你也可以直接运行命令:</para>
<screen>
nodekeeper --daemon --host localhost --port 7800
</screen>
<para>但这样只能算是一个半成品,也不够专业,我们写的是linux运用程序,必须遵循Linux规范,所有要实现一个init.d脚本</para>
<screen>
<![CDATA[
$ cat nodekeeper
#! /bin/sh
### BEGIN INIT INFO
# Provides: nodekeeper
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nodekeeper web server
# Description: starts nodekeeper using start-stop-daemon
### END INIT INFO
PATH=/srv/nodekeeper/bin:$PATH
DAEMON=/srv/nodekeeper/bin/nodekeeper
NAME=nodekeeper
DESC=nodekeeper
BASEDIR="/srv/nodekeeper"
HOST=$(ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'|head -n 1)
PORT=7800
CONFIG=$BASEDIR/etc/$NAME.cfg
LOGFILE=$BASEDIR/log/$NAME.log
PIDFILE=$BASEDIR/run/$NAME.pid
PIDFILE=/var/run/$NAME.pid
PROTOCOL=$BASEDIR/etc/protocol.cfg
DAEMON_OPTS="--daemon --host $HOST --port $PORT --config=$CONFIG --protocol=$PROTOCOL --pidfile=$PIDFILE --logfile=$LOGFILE"
test -x $DAEMON || exit 0
# Include nodekeeper defaults if available
if [ -f /etc/default/nodekeeper ] ; then
. /etc/default/nodekeeper
fi
set -e
. /lib/lsb/init-functions
#test_nodekeeper_config() {
# if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1
# then
# return 0
# else
# $DAEMON -t $DAEMON_OPTS
# return $?
# fi
#}
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON || true
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/var/run/$NAME.pid --exec $DAEMON || true
sleep 1
start-stop-daemon --start --quiet --pidfile \
/var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON || true
echo "$NAME."
;;
configtest)
echo -n "Testing $DESC configuration: "
if test_nodekeeper_config
then
echo "$NAME."
else
exit $?
fi
;;
status)
status_of_proc -p /var/run/$NAME.pid "$DAEMON" nodekeeper && exit 0 || exit $?
;;
*)
echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
exit 1
;;
esac
exit 0
]]>
</screen>
<para>我们将使用DAEMON_OPTS变量,提供所有需要的参数</para>
<screen>
DAEMON_OPTS="--daemon --host $HOST --port $PORT --config=$CONFIG --protocol=$PROTOCOL --pidfile=$PIDFILE --logfile=$LOGFILE"
</screen>
<section>
<title>start/stop</title>
<screen>
/etc/init.d/nodekeeper start
/etc/init.d/nodekeeper stop
</screen>
</section>
<section>
<title>service start/stop</title>
<screen>
service nodekeeper start
service nodekeeper stop
</screen>
</section>
</section>
</article>