{"id":115460,"date":"2019-05-22T11:17:52","date_gmt":"2019-05-22T03:17:52","guid":{"rendered":"http:\/\/www.fagao.me\/?p=115460"},"modified":"2019-05-22T11:17:52","modified_gmt":"2019-05-22T03:17:52","slug":"%e7%94%a8python%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aa%e5%a4%a7%e6%95%b0%e6%8d%ae%e6%90%9c%e7%b4%a2%e5%8f%8a%e6%ba%90%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.fagao.me\/p\/115460.htm","title":{"rendered":"\u7528Python\u5b9e\u73b0\u4e00\u4e2a\u5927\u6570\u636e\u641c\u7d22\u53ca\u6e90\u4ee3\u7801"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.fagao.me\/images\/uploads\/2019_004.jpg\" alt=\"\"><\/p>\n<div class=\"focusup\"><\/div>\n<p>\u641c\u7d22\u662f\u5927\u6570\u636e\u9886\u57df\u91cc\u5e38\u89c1\u7684\u9700\u6c42\u3002Splunk\u548cELK\u5206\u522b\u662f\u8be5\u9886\u57df\u5728\u975e\u5f00\u6e90\u548c\u5f00\u6e90\u9886\u57df\u91cc\u7684\u9886\u5bfc\u8005\u3002\u672c\u6587\u5229\u7528\u5f88\u5c11\u7684Python\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u57fa\u672c\u7684\u6570\u636e\u641c\u7d22\u529f\u80fd\uff0c\u8bd5\u56fe\u8ba9\u5927\u5bb6\u7406\u89e3\u5927\u6570\u636e\u641c\u7d22\u7684\u57fa\u672c\u539f\u7406\u3002<\/p>\n<p>\u5e03\u9686\u8fc7\u6ee4\u5668 \uff08Bloom Filter\uff09<\/p>\n<p>\u7b2c\u4e00\u6b65\u6211\u4eec\u5148\u8981\u5b9e\u73b0\u4e00\u4e2a\u5e03\u9686\u8fc7\u6ee4\u5668\u3002<\/p>\n<p>\u5e03\u9686\u8fc7\u6ee4\u5668\u662f\u5927\u6570\u636e\u9886\u57df\u7684\u4e00\u4e2a\u5e38\u89c1\u7b97\u6cd5\uff0c\u5b83\u7684\u76ee\u7684\u662f\u8fc7\u6ee4\u6389\u90a3\u4e9b\u4e0d\u662f\u76ee\u6807\u7684\u5143\u7d20\u3002\u4e5f\u5c31\u662f\u8bf4\u5982\u679c\u4e00\u4e2a\u8981\u641c\u7d22\u7684\u8bcd\u5e76\u4e0d\u5b58\u5728\u4e0e\u6211\u7684\u6570\u636e\u4e2d\uff0c\u90a3\u4e48\u5b83\u53ef\u4ee5\u4ee5\u5f88\u5feb\u7684\u901f\u5ea6\u8fd4\u56de\u76ee\u6807\u4e0d\u5b58\u5728\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u770b\u770b\u4ee5\u4e0b\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u4ee3\u7801\uff1a<\/p>\n<blockquote>\n<p>classBloomfilter(object):<\/p>\n<p>&#8220;&#8221;&#8221;<\/p>\n<p>A Bloom filter is a probabilistic data-structure that trades space for accuracy<\/p>\n<p>when determining if a value is in a set. It can tell you if a value was possibly<\/p>\n<p>added, or if it was definitely not added, but it can&#8217;t tell you for certain that<\/p>\n<p>it was added.<\/p>\n<p>&#8220;&#8221;&#8221;<\/p>\n<p>def __init__(self,size):<\/p>\n<p>&#8220;&#8221;&#8221;Setup the BF with the appropriate size&#8221;&#8221;&#8221;<\/p>\n<p>self.values= [False]* size<\/p>\n<p>self.size= size<\/p>\n<p>def hash_value(self,value):<\/p>\n<p>&#8220;&#8221;&#8221;Hash the value provided and scale it to fit the BF size&#8221;&#8221;&#8221;<\/p>\n<p>returnhash(value)% self.size<\/p>\n<p>def add_value(self,value):<\/p>\n<p>&#8220;&#8221;&#8221;Add a value to the BF&#8221;&#8221;&#8221;<\/p>\n<p>h= self.hash_value(value)<\/p>\n<p>self.values[h]= True<\/p>\n<p>def might_contain(self,value):<\/p>\n<p>&#8220;&#8221;&#8221;Check if the value might be in the BF&#8221;&#8221;&#8221;<\/p>\n<p>h= self.hash_value(value)<\/p>\n<p>returnself.values[h]<\/p>\n<p>def print_contents(self):<\/p>\n<p>&#8220;&#8221;&#8221;Dump the contents of the BF for debugging purposes&#8221;&#8221;&#8221;<\/p>\n<p>print self.values<\/p>\n<\/blockquote>\n<p>\u57fa\u672c\u7684\u6570\u636e\u7ed3\u6784\u662f\u4e2a\u6570\u7ec4\uff08\u5b9e\u9645\u4e0a\u662f\u4e2a\u4f4d\u56fe\uff0c\u75281\/0\u6765\u8bb0\u5f55\u6570\u636e\u662f\u5426\u5b58\u5728\uff09\uff0c\u521d\u59cb\u5316\u662f\u6ca1\u6709\u4efb\u4f55\u5185\u5bb9\uff0c\u6240\u4ee5\u5168\u90e8\u7f6eFalse\u3002\u5b9e\u9645\u7684\u4f7f\u7528\u5f53\u4e2d\uff0c\u8be5\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u975e\u5e38\u5927\u7684\uff0c\u4ee5\u4fdd\u8bc1\u6548\u7387\u3002<\/p>\n<p>\u5229\u7528\u54c8\u5e0c\u7b97\u6cd5\u6765\u51b3\u5b9a\u6570\u636e\u5e94\u8be5\u5b58\u5728\u54ea\u4e00\u4f4d\uff0c\u4e5f\u5c31\u662f\u6570\u7ec4\u7684\u7d22\u5f15<\/p>\n<p>\u5f53\u4e00\u4e2a\u6570\u636e\u88ab\u52a0\u5165\u5230\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u65f6\u5019\uff0c\u8ba1\u7b97\u5b83\u7684\u54c8\u5e0c\u503c\u7136\u540e\u628a\u76f8\u5e94\u7684\u4f4d\u7f6e\u4e3aTrue<\/p>\n<p>\u5f53\u68c0\u67e5\u4e00\u4e2a\u6570\u636e\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u6216\u8005\u8bf4\u88ab\u7d22\u5f15\u8fc7\u7684\u65f6\u5019\uff0c\u53ea\u8981\u68c0\u67e5\u5bf9\u5e94\u7684\u54c8\u5e0c\u503c\u6240\u5728\u7684\u4f4d\u7684True\uff0fFasle<\/p>\n<p>\u770b\u5230\u8fd9\u91cc\uff0c\u5927\u5bb6\u5e94\u8be5\u53ef\u4ee5\u770b\u51fa\uff0c\u5982\u679c\u5e03\u9686\u8fc7\u6ee4\u5668\u8fd4\u56deFalse\uff0c\u90a3\u4e48\u6570\u636e\u4e00\u5b9a\u662f\u6ca1\u6709\u7d22\u5f15\u8fc7\u7684\uff0c\u7136\u800c\u5982\u679c\u8fd4\u56deTrue\uff0c\u90a3\u4e5f\u4e0d\u80fd\u8bf4\u6570\u636e\u4e00\u5b9a\u5c31\u5df2\u7ecf\u88ab\u7d22\u5f15\u8fc7\u3002\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u5e03\u9686\u8fc7\u6ee4\u5668\u53ef\u4ee5\u4f7f\u5f97\u5f88\u591a\u6ca1\u6709\u547d\u4e2d\u7684\u641c\u7d22\u63d0\u524d\u8fd4\u56de\u6765\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<p>\u6211\u4eec\u770b\u770b\u8fd9\u6bb5 code\u662f\u5982\u4f55\u8fd0\u884c\u7684\uff1a<\/p>\n<blockquote>\n<p>bf= Bloomfilter(10)<\/p>\n<p>bf.add_value(&#8216;dog&#8217;)<\/p>\n<p>bf.add_value(&#8216;fish&#8217;)<\/p>\n<p>bf.add_value(&#8216;cat&#8217;)<\/p>\n<p>bf.print_contents()<\/p>\n<p>bf.add_value(&#8216;bird&#8217;)<\/p>\n<p>bf.print_contents()<\/p>\n<p># Note: contents are unchanged after adding bird &#8211; it collides<\/p>\n<p>forterm in[&#8216;dog&#8217;,&#8217;fish&#8217;,&#8217;cat&#8217;,&#8217;bird&#8217;,&#8217;duck&#8217;,&#8217;emu&#8217;]:<\/p>\n<p>print'{}: {} {}&#8217;.format(term,bf.hash_value(term),bf.might_contain(term))<\/p>\n<\/blockquote>\n<p>\u7ed3\u679c\uff1a<\/p>\n<blockquote>\n<p>[False,False,False,False,True,True,False,False,False,True]<\/p>\n<p>[False,False,False,False,True,True,False,False,False,True]<\/p>\n<p>dog: 5True<\/p>\n<p>fish: 4True<\/p>\n<p>cat: 9True<\/p>\n<p>bird: 9True<\/p>\n<p>duck: 5True<\/p>\n<p>emu: 8False<\/p>\n<\/blockquote>\n<p>\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5bb9\u91cf\u4e3a10\u7684\u7684\u5e03\u9686\u8fc7\u6ee4\u5668<\/p>\n<\/p>\n<p>\u7136\u540e\u5206\u522b\u52a0\u5165 \u2018dog\u2019\uff0c\u2018fish\u2019\uff0c\u2018cat\u2019\u4e09\u4e2a\u5bf9\u8c61\uff0c\u8fd9\u65f6\u7684\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p>\u7136\u540e\u52a0\u5165\u2018bird\u2019\u5bf9\u8c61\uff0c\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u5185\u5bb9\u5e76\u6ca1\u6709\u6539\u53d8\uff0c\u56e0\u4e3a\u2018bird\u2019\u548c\u2018fish\u2019\u6070\u597d\u62e5\u6709\u76f8\u540c\u7684\u54c8\u5e0c\u3002<\/p>\n<\/p>\n<p>\u6700\u540e\u6211\u4eec\u68c0\u67e5\u4e00\u5806\u5bf9\u8c61\uff08\u2019dog\u2019, \u2018fish\u2019, \u2018cat\u2019, \u2018bird\u2019, \u2018duck\u2019, \u2019emu\u2019\uff09\u662f\u4e0d\u662f\u5df2\u7ecf\u88ab\u7d22\u5f15\u4e86\u3002\u7ed3\u679c\u53d1\u73b0\u2018duck\u2019\u8fd4\u56deTrue\uff0c2\u800c\u2018emu\u2019\u8fd4\u56deFalse\u3002\u56e0\u4e3a\u2018duck\u2019\u7684\u54c8\u5e0c\u6070\u597d\u548c\u2018dog\u2019\u662f\u4e00\u6837\u7684\u3002<\/p>\n<\/p>\n<p>\u5206\u8bcd<\/p>\n<p>\u4e0b\u9762\u4e00\u6b65\u6211\u4eec\u8981\u5b9e\u73b0\u5206\u8bcd\u3002 \u5206\u8bcd\u7684\u76ee\u7684\u662f\u8981\u628a\u6211\u4eec\u7684\u6587\u672c\u6570\u636e\u5206\u5272\u6210\u53ef\u641c\u7d22\u7684\u6700\u5c0f\u5355\u5143\uff0c\u4e5f\u5c31\u662f\u8bcd\u3002\u8fd9\u91cc\u6211\u4eec\u4e3b\u8981\u9488\u5bf9\u82f1\u8bed\uff0c\u56e0\u4e3a\u4e2d\u6587\u7684\u5206\u8bcd\u6d89\u53ca\u5230\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff0c\u6bd4\u8f83\u590d\u6742\uff0c\u800c\u82f1\u6587\u57fa\u672c\u53ea\u8981\u7528\u6807\u70b9\u7b26\u53f7\u5c31\u597d\u4e86\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u770b\u770b\u5206\u8bcd\u7684\u4ee3\u7801\uff1a<\/p>\n<blockquote>\n<p>def major_segments(s):<\/p>\n<p>&#8220;&#8221;&#8221;<\/p>\n<p>Perform major segmenting on a string. Split the string by all of the major<\/p>\n<p>breaks, and return the set of everything found. The breaks in this implementation<\/p>\n<p>are single characters, but in Splunk proper they can be multiple characters.<\/p>\n<p>A set is used because ordering doesn&#8217;t matter, and duplicates are bad.<\/p>\n<p>&#8220;&#8221;&#8221;<\/p>\n<p>major_breaks= &#8216; &#8216;<\/p>\n<p>last= -1<\/p>\n<p>results= set()<\/p>\n<p># enumerate() will give us (0, s[0]), (1, s[1]), &#8230;<\/p>\n<p>foridx,ch inenumerate(s):<\/p>\n<p>ifch inmajor_breaks:<\/p>\n<p>segment= s[last+1:idx]<\/p>\n<p>results.add(segment)<\/p>\n<p>last= idx<\/p>\n<p># The last character may not be a break so always capture<\/p>\n<p># the last segment (which may end up being &#8220;&#8221;, but yolo)<\/p>\n<p>segment= s[last+1:]<\/p>\n<p>results.add(segment)<\/p>\n<p>returnresults<\/p>\n<\/blockquote>\n<p>\u4e3b\u8981\u5206\u5272<\/p>\n<p>\u4e3b\u8981\u5206\u5272\u4f7f\u7528\u7a7a\u683c\u6765\u5206\u8bcd\uff0c\u5b9e\u9645\u7684\u5206\u8bcd\u903b\u8f91\u4e2d\uff0c\u8fd8\u4f1a\u6709\u5176\u5b83\u7684\u5206\u9694\u7b26\u3002\u4f8b\u5982Splunk\u7684\u7f3a\u7701\u5206\u5272\u7b26\u5305\u62ec\u4ee5\u4e0b\u8fd9\u4e9b\uff0c\u7528\u6237\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u5206\u5272\u7b26\u3002<\/p>\n<p>] &lt; &gt;( ) { } | ! ; , \u2018 \u201d * s &amp; ? + %21 %26 %2526 %3B %7C %20 %2B %3D \u2014 %2520 %5D %5B %3A %0A %2C %28 %29<\/p>\n<blockquote>\n<p>def minor_segments(s):<\/p>\n<p>&#8220;&#8221;&#8221;<\/p>\n<p>Perform minor segmenting on a string. This is like major<\/p>\n<p>segmenting, except it also captures from the start of the<\/p>\n<p>input to each break.<\/p>\n<p>&#8220;&#8221;&#8221;<\/p>\n<p>minor_breaks= &#8216;_.&#8217;<\/p>\n<p>last= -1<\/p>\n<p>results= set()<\/p>\n<p>foridx,ch inenumerate(s):<\/p>\n<p>ifch inminor_breaks:<\/p>\n<p>segment= s[last+1:idx]<\/p>\n<p>results.add(segment)<\/p>\n<p>segment= s[:idx]<\/p>\n<p>results.add(segment)<\/p>\n<p>last= idx<\/p>\n<p>segment= s[last+1:]<\/p>\n<p>results.add(segment)<\/p>\n<p>results.add(s)<\/p>\n<p>returnresults<\/p>\n<\/blockquote>\n<p>\u6b21\u8981\u5206\u5272<\/p>\n<p>\u6b21\u8981\u5206\u5272\u548c\u4e3b\u8981\u5206\u5272\u7684\u903b\u8f91\u7c7b\u4f3c\uff0c\u53ea\u662f\u8fd8\u4f1a\u628a\u4ece\u5f00\u59cb\u90e8\u5206\u5230\u5f53\u524d\u5206\u5272\u7684\u7ed3\u679c\u52a0\u5165\u3002\u4f8b\u5982\u201c1.2.3.4\u201d\u7684\u6b21\u8981\u5206\u5272\u4f1a\u67091\uff0c2\uff0c3\uff0c4\uff0c1.2\uff0c1.2.3<\/p>\n<blockquote>\n<p>def segments(event):<\/p>\n<p>&#8220;&#8221;&#8221;Simple wrapper around major_segments \/ minor_segments&#8221;&#8221;&#8221;<\/p>\n<p>results= set()<\/p>\n<p>formajor inmajor_segments(event):<\/p>\n<p>forminor inminor_segments(major):<\/p>\n<p>results.add(minor)<\/p>\n<p>returnresults<\/p>\n<\/blockquote>\n<p>\u5206\u8bcd\u7684\u903b\u8f91\u5c31\u662f\u5bf9\u6587\u672c\u5148\u8fdb\u884c\u4e3b\u8981\u5206\u5272\uff0c\u5bf9\u6bcf\u4e00\u4e2a\u4e3b\u8981\u5206\u5272\u5728\u8fdb\u884c\u6b21\u8981\u5206\u5272\u3002\u7136\u540e\u628a\u6240\u6709\u5206\u51fa\u6765\u7684\u8bcd\u8fd4\u56de\u3002<\/p>\n<p>\u6211\u4eec\u770b\u770b\u8fd9\u6bb5 code\u662f\u5982\u4f55\u8fd0\u884c\u7684\uff1a<\/p>\n<blockquote>\n<p>forterm insegments(&#8216;src_ip = 1.2.3.4&#8217;):<\/p>\n<p>print term<\/p>\n<\/blockquote>\n<blockquote>\n<p>src<\/p>\n<p>1.2<\/p>\n<p>1.2.3.4<\/p>\n<p>src_ip<\/p>\n<p>3<\/p>\n<p>1<\/p>\n<p>1.2.3<\/p>\n<p>ip<\/p>\n<p>2<\/p>\n<p>=<\/p>\n<p>4<\/p>\n<\/blockquote>\n<p>\u641c\u7d22<\/p>\n<p>\u597d\u4e86\uff0c\u6709\u4e2a\u5206\u8bcd\u548c\u5e03\u9686\u8fc7\u6ee4\u5668\u8fd9\u4e24\u4e2a\u5229\u5668\u7684\u652f\u6491\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u6765\u5b9e\u73b0\u641c\u7d22\u7684\u529f\u80fd\u4e86\u3002<\/p>\n<p>\u4e0a\u4ee3\u7801\uff1a<\/p>\n<blockquote>\n<p>classSplunk(object):<\/p>\n<p>def __init__(self):<\/p>\n<p>self.bf= Bloomfilter(64)<\/p>\n<p>self.terms= {}# Dictionary of term to set of events<\/p>\n<p>self.events= []<\/p>\n<p>def add_event(self,event):<\/p>\n<p>&#8220;&#8221;&#8221;Adds an event to this object&#8221;&#8221;&#8221;<\/p>\n<p># Generate a unique ID for the event, and save it<\/p>\n<p>event_id= len(self.events)<\/p>\n<p>self.events.append(event)<\/p>\n<p># Add each term to the bloomfilter, and track the event by each term<\/p>\n<p>forterm insegments(event):<\/p>\n<p>self.bf.add_value(term)<\/p>\n<p>ifterm notinself.terms:<\/p>\n<p>self.terms[term]= set()<\/p>\n<p>self.terms[term].add(event_id)<\/p>\n<p>def search(self,term):<\/p>\n<p>&#8220;&#8221;&#8221;Search for a single term, and yield all the events that contain it&#8221;&#8221;&#8221;<\/p>\n<p># In Splunk this runs in O(1), and is likely to be in filesystem cache (memory)<\/p>\n<p>ifnotself.bf.might_contain(term):<\/p>\n<p>return<\/p>\n<p># In Splunk this probably runs in O(log N) where N is the number of terms in the tsidx<\/p>\n<p>ifterm notinself.terms:<\/p>\n<p>return<\/p>\n<p>forevent_id insorted(self.terms[term]):<\/p>\n<p>yield self.events[event_id]<\/p>\n<\/blockquote>\n<p>Splunk\u4ee3\u8868\u4e00\u4e2a\u62e5\u6709\u641c\u7d22\u529f\u80fd\u7684\u7d22\u5f15\u96c6\u5408<\/p>\n<p>\u6bcf\u4e00\u4e2a\u96c6\u5408\u4e2d\u5305\u542b\u4e00\u4e2a\u5e03\u9686\u8fc7\u6ee4\u5668\uff0c\u4e00\u4e2a\u5012\u6392\u8bcd\u8868\uff08\u5b57\u5178\uff09\uff0c\u548c\u4e00\u4e2a\u5b58\u50a8\u6240\u6709\u4e8b\u4ef6\u7684\u6570\u7ec4<\/p>\n<p>\u5f53\u4e00\u4e2a\u4e8b\u4ef6\u88ab\u52a0\u5165\u5230\u7d22\u5f15\u7684\u65f6\u5019\uff0c\u4f1a\u505a\u4ee5\u4e0b\u7684\u903b\u8f91<\/p>\n<p>\u4e3a\u6bcf\u4e00\u4e2a\u4e8b\u4ef6\u751f\u6210\u4e00\u4e2aunqie id\uff0c\u8fd9\u91cc\u5c31\u662f\u5e8f\u53f7<\/p>\n<p>\u5bf9\u4e8b\u4ef6\u8fdb\u884c\u5206\u8bcd\uff0c\u628a\u6bcf\u4e00\u4e2a\u8bcd\u52a0\u5165\u5230\u5012\u6392\u8bcd\u8868\uff0c\u4e5f\u5c31\u662f\u6bcf\u4e00\u4e2a\u8bcd\u5bf9\u5e94\u7684\u4e8b\u4ef6\u7684id\u7684\u6620\u5c04\u7ed3\u6784\uff0c\u6ce8\u610f\uff0c\u4e00\u4e2a\u8bcd\u53ef\u80fd\u5bf9\u5e94\u591a\u4e2a\u4e8b\u4ef6\uff0c\u6240\u4ee5\u5012\u6392\u8868\u7684\u7684\u503c\u662f\u4e00\u4e2aSet\u3002\u5012\u6392\u8868\u662f\u7edd\u5927\u90e8\u5206<a class=\"tag-links\" href=\"https:\/\/www.fagao.me\/p\/t\/121\">\u641c\u7d22\u5f15\u64ce<\/a>\u7684\u6838\u5fc3\u529f\u80fd\u3002<\/p>\n<p>\u5f53\u4e00\u4e2a\u8bcd\u88ab\u641c\u7d22\u7684\u65f6\u5019\uff0c\u4f1a\u505a\u4ee5\u4e0b\u7684\u903b\u8f91<\/p>\n<p>\u68c0\u67e5\u5e03\u9686\u8fc7\u6ee4\u5668\uff0c\u5982\u679c\u4e3a\u5047\uff0c\u76f4\u63a5\u8fd4\u56de<\/p>\n<p>\u68c0\u67e5\u8bcd\u8868\uff0c\u5982\u679c\u88ab\u641c\u7d22\u5355\u8bcd\u4e0d\u5728\u8bcd\u8868\u4e2d\uff0c\u76f4\u63a5\u8fd4\u56de<\/p>\n<p>\u5728\u5012\u6392\u8868\u4e2d\u627e\u5230\u6240\u6709\u5bf9\u5e94\u7684\u4e8b\u4ef6id\uff0c\u7136\u540e\u8fd4\u56de\u4e8b\u4ef6\u7684\u5185\u5bb9<\/p>\n<p>\u6211\u4eec\u8fd0\u884c\u4e0b\u770b\u770b\u628a\uff1a<\/p>\n<blockquote>\n<p>s= Splunk()<\/p>\n<p>s.add_event(&#8216;src_ip = 1.2.3.4&#8217;)<\/p>\n<p>s.add_event(&#8216;src_ip = 5.6.7.8&#8217;)<\/p>\n<p>s.add_event(&#8216;dst_ip = 1.2.3.4&#8217;)<\/p>\n<p>forevent ins.search(&#8216;1.2.3.4&#8217;):<\/p>\n<p>print event<\/p>\n<p>print&#8217;-&#8216;<\/p>\n<p>forevent ins.search(&#8216;src_ip&#8217;):<\/p>\n<p>print event<\/p>\n<p>print&#8217;-&#8216;<\/p>\n<p>forevent ins.search(&#8216;ip&#8217;):<\/p>\n<p>print event<\/p>\n<\/blockquote>\n<blockquote>\n<p>src_ip= 1.2.3.4<\/p>\n<p>dst_ip= 1.2.3.4<\/p>\n<p>&#8211;<\/p>\n<p>src_ip= 1.2.3.4<\/p>\n<p>src_ip= 5.6.7.8<\/p>\n<p>&#8211;<\/p>\n<p>src_ip= 1.2.3.4<\/p>\n<p>src_ip= 5.6.7.8<\/p>\n<p>dst_ip= 1.2.3.4<\/p>\n<\/blockquote>\n<p>\u662f\u4e0d\u662f\u5f88\u8d5e\uff01<\/p>\n<p>\u66f4\u590d\u6742\u7684\u641c\u7d22<\/p>\n<p>\u66f4\u8fdb\u4e00\u6b65\uff0c\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u60f3\u7528And\u548cOr\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u641c\u7d22\u903b\u8f91\u3002<\/p>\n<p>\u4e0a\u4ee3\u7801\uff1a<\/p>\n<blockquote>\n<p>classSplunkM(object):<\/p>\n<p>def __init__(self):<\/p>\n<p>self.bf= Bloomfilter(64)<\/p>\n<p>self.terms= {}# Dictionary of term to set of events<\/p>\n<p>self.events= []<\/p>\n<p>def add_event(self,event):<\/p>\n<p>&#8220;&#8221;&#8221;Adds an event to this object&#8221;&#8221;&#8221;<\/p>\n<p># Generate a unique ID for the event, and save it<\/p>\n<p>event_id= len(self.events)<\/p>\n<p>self.events.append(event)<\/p>\n<p># Add each term to the bloomfilter, and track the event by each term<\/p>\n<p>forterm insegments(event):<\/p>\n<p>self.bf.add_value(term)<\/p>\n<p>ifterm notinself.terms:<\/p>\n<p>self.terms[term]= set()<\/p>\n<p>self.terms[term].add(event_id)<\/p>\n<p>def search_all(self,terms):<\/p>\n<p>&#8220;&#8221;&#8221;Search for an AND of all terms&#8221;&#8221;&#8221;<\/p>\n<p># Start with the universe of all events&#8230;<\/p>\n<p>results= set(range(len(self.events)))<\/p>\n<p>forterm interms:<\/p>\n<p># If a term isn&#8217;t present at all then we can stop looking<\/p>\n<p>ifnotself.bf.might_contain(term):<\/p>\n<p>return<\/p>\n<p>ifterm notinself.terms:<\/p>\n<p>return<\/p>\n<p># Drop events that don&#8217;t match from our results<\/p>\n<p>results= results.intersection(self.terms[term])<\/p>\n<p>forevent_id insorted(results):<\/p>\n<p>yield self.events[event_id]<\/p>\n<p>def search_any(self,terms):<\/p>\n<p>&#8220;&#8221;&#8221;Search for an OR of all terms&#8221;&#8221;&#8221;<\/p>\n<p>results= set()<\/p>\n<p>forterm interms:<\/p>\n<p># If a term isn&#8217;t present, we skip it, but don&#8217;t stop<\/p>\n<p>ifnotself.bf.might_contain(term):<\/p>\n<p>continue<\/p>\n<p>ifterm notinself.terms:<\/p>\n<p>continue<\/p>\n<p># Add these events to our results<\/p>\n<p>results= results.union(self.terms[term])<\/p>\n<p>forevent_id insorted(results):<\/p>\n<p>yield self.events[event_id]<\/p>\n<\/blockquote>\n<p>\u5229\u7528Python\u96c6\u5408\u7684intersection\u548cunion\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u652f\u6301And\uff08\u6c42\u4ea4\u96c6\uff09\u548cOr\uff08\u6c42\u5408\u96c6\uff09\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<blockquote>\n<p>s= SplunkM()<\/p>\n<p>s.add_event(&#8216;src_ip = 1.2.3.4&#8217;)<\/p>\n<p>s.add_event(&#8216;src_ip = 5.6.7.8&#8217;)<\/p>\n<p>s.add_event(&#8216;dst_ip = 1.2.3.4&#8217;)<\/p>\n<p>forevent ins.search_all([&#8216;src_ip&#8217;,&#8217;5.6&#8242;]):<\/p>\n<p>print event<\/p>\n<p>print&#8217;-&#8216;<\/p>\n<p>forevent ins.search_any([&#8216;src_ip&#8217;,&#8217;dst_ip&#8217;]):<\/p>\n<p>print event<\/p>\n<\/blockquote>\n<blockquote>\n<p>src_ip= 5.6.7.8<\/p>\n<p>&#8211;<\/p>\n<p>src_ip= 1.2.3.4<\/p>\n<p>src_ip= 5.6.7.8<\/p>\n<p>dst_ip= 1.2.3.4<\/p>\n<\/blockquote>\n<p>\u603b\u7ed3<\/p>\n<p>\u4ee5\u4e0a\u7684\u4ee3\u7801\u53ea\u662f\u4e3a\u4e86\u8bf4\u660e\u5927\u6570\u636e\u641c\u7d22\u7684\u57fa\u672c\u539f\u7406\uff0c\u5305\u62ec\u5e03\u9686\u8fc7\u6ee4\u5668\uff0c\u5206\u8bcd\u548c\u5012\u6392\u8868\u3002\u5982\u679c\u5927\u5bb6\u771f\u7684\u60f3\u8981\u5229\u7528\u8fd9\u4ee3\u7801\u6765\u5b9e\u73b0\u771f\u6b63\u7684\u641c\u7d22\u529f\u80fd\uff0c\u8fd8\u5dee\u7684\u592a\u8fdc\u3002\u6240\u6709\u7684\u5185\u5bb9\u6765\u81ea\u4e8eSplunk Conf2017\u3002\u5927\u5bb6\u5982\u679c\u6709\u5174\u8da3\u53ef\u4ee5\u53bb\u770b\u7f51\u4e0a\u7684\u89c6\u9891\u3002<\/p>\n<p>\u6765\u6e90\uff1a51CTO\u535a\u5ba2<\/p>\n<p>\u4f5c\u8005\uff1a\u521a\u662f\u7a0b\u5e8f\u5458<\/p>\n<p>\u5b66\u6821\uff1a\u8a89\u5929\u6559\u80b2<\/p>\n<p>QQ\uff1a3200569443<\/p>\n<p>\u5fae\u4fe1\uff1a13487088610<\/p>\n<div class=\"articleend\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u641c\u7d22\u662f\u5927\u6570\u636e\u9886\u57df\u91cc\u5e38\u89c1\u7684\u9700\u6c42\u3002Splunk\u548cELK\u5206\u522b\u662f\u8be5\u9886\u57df\u5728\u975e\u5f00\u6e90\u548c\u5f00\u6e90\u9886\u57df\u91cc\u7684\u9886\u5bfc\u8005\u3002\u672c\u6587\u5229\u7528\u5f88\u5c11\u7684Py [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[183],"class_list":["post-115460","post","type-post","status-publish","format-standard","hentry","category-news","tag-183"],"_links":{"self":[{"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/posts\/115460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/comments?post=115460"}],"version-history":[{"count":0,"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/posts\/115460\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/media?parent=115460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/categories?post=115460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/tags?post=115460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}