NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66...

132
NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford

Transcript of NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66...

Page 1: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

NetOps Coding 201auto remediation for your network!

NANOG 66

02.10.2016

david swafford

Page 2: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

continuing our theme

2

Page 3: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

automating

3

Page 4: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

the remediation of network faults

4

Page 5: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

based on parsing of syslog messages

5

Page 6: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

focusing today

6

Page 7: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

on building the system

7

Page 8: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

8

goals• organized to support 100s of

remediations

• fast enough to react to 100s of events at at time

• simple enough to manage without a CS background

Page 9: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

how it works

9

Page 10: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

10

device B

device A

Ethernet

Page 11: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

Ethernet

11

device

device link down!X device B

device A

Page 12: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

12

syslog server

sendsmessage

1

device

device X

%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet5/1isdown(Linkfailure)

Page 13: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

13

syslog server

sendsmessage

parser

tails messages

1

2

device

device X

%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet5/1isdown(Linkfailure)

Page 14: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

14

syslog server

sendsmessage

parser

tails messages

1

2

device

device X

regex convertsmessage into data

Page 15: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

15

syslog server

sendsmessage

parser dbcreates

event

tails messages

1

23

device

device X

Page 16: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

16

syslog server

sendsmessage

parser dbcreates

event

event processor

gets

event

tails messages

1

23 4

device

device X

Page 17: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

17

syslog server

sendsmessage

parser dbcreates

event

event processor

gets

event

tails messages

1

23 4

remediation

runs5

device

device X

Page 18: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

18

syslog server

sendsmessage

parser dbcreates

event

event processor

gets

event

tails messages

1

23 4

remediation

runs5

remediates6device

device X

Page 19: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

19

syslog server

sendsmessage

parser dbcreates

event

event processor

gets

event

tails messages

1

23 4

remediation

runs5

remediates6

saves result

7

device

device X

Page 20: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

remediates

20

syslog server

sendsmessage

parsercreates

event

tails messages

1

23

6device

device Xtoday's focus

dbevent

processorgets

event

4

remediation

runs5

saves result

7

Page 21: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

demo!

21

Page 22: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

22

syslog generator

Page 23: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

23

syslog generator

Page 24: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

24

syslog generator

Page 25: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

25

syslog generator

Page 26: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

26

syslog parser

Page 27: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

27

syslog parser

Page 28: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

28

syslog parser

Page 29: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

29

event processor

Page 30: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

30

event processor

Page 31: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

31

event processor

Page 32: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

32

event processor

Page 33: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

33

event processor

Page 34: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

the lab environment

34

Page 35: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

a virtual machine of Ubuntu desktop

35

Page 36: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

36

customized with

iPython! and extras

Python 2.7.11 & 3.5.1 MySQL server & Python libs

Page 37: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

37

importing the virtual appliance

Page 38: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

38

download it from netengcode.com!

Page 39: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

39

Page 40: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

40

Page 41: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

41

Page 42: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

42

Page 43: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

43

TERMINAL

log-in details

user:demopass:demo

Page 44: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

44

keyboard shortcuts to break out of the VM

f enter or leave full-screen

Page 45: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

getting started

45

Page 46: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

organizing our data

46

Page 47: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

47

the event as a tuple

Page 48: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

48

the event as a tuple

Page 49: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

49

what's the problem?

Page 50: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

50

what's the problem?new field!

data formats will change

Page 51: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

51

the event as a dictionary

Page 52: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

52

the event as an object

link

Page 53: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

53

accessing attributes of an object

link

Page 54: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

which to choose?

54

Page 55: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

55

the advantage of an object

enforcement of structure

Page 56: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

56

the advantage of an object

input validation (not shown)

Page 57: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

57

where to find it?

:Event()

Page 58: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

• a database (MySQL)

• an events table

• a library for managing our data

• installation of "netfbar"(a Python package)

58

what's already staged?

Page 59: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

59

database setup

Page 60: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

60

netfbar package

Page 61: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

the parser

61

Page 62: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

62

where to find it?

Page 63: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

remediations, refactored.

63

Page 64: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

64

a few problems

• duplication

• no structure

• limited debug-ability

Page 65: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

organizing our remediations

65

Page 66: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

66

"sparse is better than dense"

1 remediation, 1 file.

Page 67: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

...remediations, v2.

67

code that repeats in each remediation

Page 68: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

68

moved to a base remediation

remediations, v2.

Page 69: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

69

from base.BaseRemediation

remediations, v2.

Page 70: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

connecting remediations to error codes

70

Page 71: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

71a dictionary of "error_code" to "module"

connecting remediations to error codes

Page 72: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

72

connecting remediations to error codes

Page 73: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

73

where to find it?

Page 74: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

questions?

74

Page 75: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

building the event processor

75

Page 76: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

starting simple

76

Page 77: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

77

building it live

Page 78: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

78

building it live

Page 79: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

79

building it live

Page 80: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

80

building it live

Page 81: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

81

building it live

Page 82: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

82

no events?

Page 83: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

83

building it live

Page 84: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

84

building it live

Page 85: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

85

building it live

Page 86: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

86

building it live

Page 87: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

87

building it live

Page 88: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

88

building it live

Page 89: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

89

building it live

Page 90: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

90

building it live

Page 91: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

91

building it live

Page 92: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

92

building it live

(no logging in the default remediation)

Page 93: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

93

building it live

create a function for handling one event

Page 94: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

94

building it live

iterating through events

Page 95: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

95

building it live

so slow!

Page 96: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

questions?

96

Page 97: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

making this a little faster

97

Page 98: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

• threading • multiprocessing • asyncio • gevent • go...

98

so many choices!

Page 99: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

starting simple!

99

Page 100: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

threading & multiprocessing

100

Page 101: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

101

threading's pros

https://docs.python.org/3/library/threading.html

• simple API

• lightweight

• shared memory with the parent

Page 102: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

102

threading's cons

https://docs.python.org/3/library/threading.html

• not true parallelism

• all threads are limited to a single CPU - the parent's

Page 103: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

103

threads are evil! ....?!

https://docs.python.org/3/library/threading.html

"In CPython, due to the [GIL], only one thread can execute Python code at once"

...

"However, threading is still an appropriate model if you want to run multiple

I/O-bound tasks simultaneously."

Page 104: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

104

multiprocessing's pros

https://docs.python.org/3/library/threading.html

• simple API

• true parallelism using all CPUs

Page 105: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

105

multiprocessing's cons

https://docs.python.org/3/library/threading.html

• based on forking (cloning/copying the current process)

• heavier memory footprint

• stale memory / state

Page 106: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

106

multiprocessing's cons

https://docs.python.org/3/library/threading.html

• no shared memory with parent

Page 107: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

working with threads

107

Page 108: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

108

working with threads

https://docs.python.org/3/library/threading.html

Page 109: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

109

working with threads

https://docs.python.org/3/library/threading.html

target - the method to run inside the thread

Page 110: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

110

working with threads

https://docs.python.org/3/library/threading.html

args - the values passed as input to process_event()

Page 111: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

111

working with threads

https://docs.python.org/3/library/threading.html

.start() - spins up the thread in the background (the for loop continues)

Page 112: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

112

working with threads

https://docs.python.org/3/library/threading.html

caution! this is dangerous! (starting an unknown number of threads)

Page 113: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

113

adding visibility to process_event()

Page 114: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

114

starting our threads again (with visibility)

https://docs.python.org/3/library/threading.html

Page 115: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

115

starting our threads again (with visibility)

https://docs.python.org/3/library/threading.html

oh my! ...that just started 1020 threads!

Page 116: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

creating a thread pool

116

Page 117: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

117

creating a thread pool

the work to perform inside a thread

Page 118: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

118

creating a thread pool

starting the background threads

Page 119: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

119

creating a thread pool

looking at active threads

Page 120: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

120

creating a thread pool

looking at active threads

Page 121: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

121

creating a thread pool

fetching events that we'll send to the threads

Page 122: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

122

creating a thread pool

Page 123: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

123

creating a thread pool

https://docs.python.org/3/library/threading.html

creating a queue to pass events to threads

Page 124: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

124

creating a thread pool

putting the pieces together

Page 125: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

125

creating a thread pool

putting the pieces together

...

Page 126: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

126

creating a thread pool

now, continuously!

Page 127: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

127

creating a thread pool

Page 128: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

questions?

128

Page 129: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

closing out

129

Page 130: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

130

syslog server

sendsmessage

parser dbcreates

event

event processor

gets

event

tails messages

1

23 4

remediation

runs5

remediates6

saves result

7

device

device X

Page 131: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

131

goals• organized to support 100s of

remediations

• fast enough to react to 100s of events at at time

• simple enough to manage without a CS background

Page 132: NetOps Coding 201 - NANOG Archive · NetOps Coding 201 auto remediation for your network! NANOG 66 02.10.2016 david swafford. continuing our theme 2. automating 3. the remediation

NANOG 66

02.10.2016

david swafford

#netengcode